开发者

Check files in current directory

开发者 https://www.devze.com 2023-04-07 04:35 出处:网络
How can I tell python to scan the current directory for a file called \"filenames.txt\" and if that file isn\'t there, 开发者_开发问答to extract it from a zip file called \"files.zip\"? I know how to

How can I tell python to scan the current directory for a file called "filenames.txt" and if that file isn't there, 开发者_开发问答to extract it from a zip file called "files.zip"? I know how to work zipfile, I just don't know how to scan the current directory for that file and use if/then loops with it..


import os.path
try:    
    os.path.isFile(fname)
    # play with the file
except:
    # unzip file


import os, zipfile
if 'filenames.txt' in os.listdir('.'):
    print 'file is in current dir'
else:
    zf = zipfile.ZipFile('files.zip')
    zf.extract('filenames.txt')


From the documentation

$ pydoc os.path.exists
Help on function exists in os.path:

os.path.exists = exists(path)
    Test whether a path exists.  Returns False for broken symbolic links
0

精彩评论

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

关注公众号