简体   繁体   中英

File io error Python

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" 

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):
 ....

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM