开发者

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('\开发者_Go百科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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜