tags:

views:

97

answers:

3

I have a program that monitors a folder with word documents for any modifications made on the files. The error -Windows Error[2] The system cannot find the file specified- comes when I run the program, open a .doc within the folder make some changes and save it. Any suggestions on how to fix this?

Edit1: the actual error code is like this

File "C:\Users\keinsfield\Desktop\docu.py", line 27, in check
   if info[0]==os.stat(os.path.join(r"C:\Users\keinsfield\Desktop\colegio",file
).st_ctime:
WindowsError: [Error 2] The system cannot find the file specified: 'C:\\Users\\k
insfield\\Desktop\\colegio\\~WRD1761.tmp'

Here's the code:

def archivar():
    txt = open('archivo.txt', 'r+' )
    for rootdir, dirs, files in os.walk(r"C:\Users\keinsfield\Desktop\colegio"):
        for file in files:
            time  = os.stat(os.path.join(rootdir, file)).st_ctime
            txt.write(file +','+str(time) + '\n')
def check():
    txt = [col.split(',') for col in (open('archivo.txt', 'r+').read().split('\n'))]
    files = os.listdir(r"C:\Users\keinsfield\Desktop\colegio")  
    for file in files:
        for info in txt:
                if info[0]==os.stat(os.path.join(r"C:\Users\keinsfield\Desktop\colegio",file)).st_ctime:
                   print "modified" 
A: 

In your call to os.stat(os.path.join(r"C:\Users\keinsfield\Desktop\colegio",file)) and to os.walk(r"C:\Users\keinsfield\Desktop\colegio"), you don't escape the backslash(\).

In a string, the backslash is used as an escape character. For example, '\t' is a tab, not \t. If you wanted the backslash (as you do in the case of filepaths), then you will need to escape the backslash by doing \

Hope this helps

inspectorG4dget
That's why he used raw strings (`r"..."`) where you don't have to escape backslashes.
Tim Pietzcker
... because the 'r' does the escaping *for* you:>>> r"C:\Users\keinsfield\Desktop\colegio"'C:\\Users\\keinsfield\\Desktop\\colegio'
wescpy
A: 

try to use os.path.join() eg

root="c:\\"
path=os.path.join(root,"Users","keinsfield","Desktop","colegio")
....
 for rootdir, dirs, files in os.walk(path):
 ....
ghostdog74
A: 

I think from the traceback it's quite clear that the temporary file was deleted between os.walk and os.stat calls. You don't really need to use the os.walk if you're not recursing into the subdirectories. You could use glob.iglob to obtain the list of only doc files:

for file in glob.iglob(os.path.join(root, '*.doc')):
    print(file)
SilentGhost
I did this and still it doesn't work, but thanks for the new info. The only solution I have found for the problem is delaying the check function some time.
serpiente
@serpiente: in your `check` function you also need to use `glob.iglob`. you don't need all the files that `os.listdir` returns, but only doc files.
SilentGhost