【发布时间】:2017-10-19 17:15:01
【问题描述】:
我有一个 python 脚本,它查看文件夹中的所有文件以查找特定单词并将该单词替换为空格。我不想在每次运行脚本后更改要查看的单词,而是继续为脚本添加新单词以查找并执行相同的替换操作。
我在 macOS El Capitan 上运行它。下面是脚本:
import os
paths = (os.path.join(root, filename)
for root, _, filenames in os.walk('/Users/Test/Desktop/Test')
for filename in filenames)
for path in paths:
# the '#' in the example below will be replaced by the '-' in the filenames in the directory
newname = path.replace('.File',' ')
if newname != path:
os.rename(path, newname)
for path in paths:
# the '#' in the example below will be replaced by the '-' in the filenames in the directory
newname = path.replace('Generic',' ')
if newname != path:
os.rename(path, newname)
您可以为这个新手提供任何帮助,我们将不胜感激。
【问题讨论】: