小编Ben*_* A.的帖子

为什么这个Python脚本会删除'.txt'文件,以及所有'.tmp'文件?

我正在尝试编写一个脚本,它会自动删除特定文件夹中的所有临时文件,我注意到这个脚本也会删除该文件夹中的所有文本文件.任何人都可以解释为什么这样做?

   import os
   path = 'C:\scripts27'
   for root, dirs, files in os.walk(path):
       for currentFile in files:
           print "processing file: " + currentFile
           extensions=('.tmp')
           if any(currentFile.lower().endswith(ext) for ext in extensions):
               os.remove(os.path.join(root, currentFile))
Run Code Online (Sandbox Code Playgroud)

我在Windows 8.1 PC 64位上使用Python 2.7.10运行此脚本.

谢谢!

python os.walk python-2.7 os.path

3
推荐指数
1
解决办法
337
查看次数

标签 统计

os.path ×1

os.walk ×1

python ×1

python-2.7 ×1