替换列表中的部分文件名 (Python)
Replace part of filename if it's found in a list (Python)
我有一段代码,其中大部分是我从这里的另一个问题中得到的。做了一些调整后,它似乎并没有工作。
目的很简单:我有一个包含很多文件的目录,所有文件都包含垃圾words/characters,需要用不同的东西替换。
我有 2 个包含这些词的列表。代码如下所示:
import os, string
rootdir = r'C:\Users\me\Downloads\Documents\Folder'
lst = ["this that", "This.that", "THIS.THAT", "this_that", "This.That"]
lst2 = ["_","__","___","."]
for filename in os.listdir(rootdir):
if any(i in filename for i in lst):
filepath = os.path.join(rootdir, filename)
newfilepath = os.path.join(rootdir, filename.replace(i, ""))
os.rename(filepath, newfilepath)
if any(i in filename for i in lst2):
filepath = os.path.join(rootdir, filename)
newfilepath = os.path.join(rootdir, filename.replace(i, " "))
os.rename(filepath, newfilepath)
但是当我 运行 时,错误是 NameError: name 'i' is not defined
不知道为什么会这样,我应该被定义为“出现在列表中的文件名的一部分”。至少我是这么认为的。尝试用不同的方式定义它,但没有成功。
有什么解决办法吗?谢谢
正如 han solo 和 Vaibhav 所指出的,变量 i 只存在于 for 循环的范围内。由于您使用单行 for 循环,因此无法在其余代码中访问该变量。我建议像这样使用多行 for 循环:
import os, string
rootdir = r'C:\Users\me\Downloads\Documents\Folder'
lst = ["this that", "This.that", "THIS.THAT", "this_that", "This.That"]
lst2 = ["_","__","___","."]
for filename in os.listdir(rootdir):
for i in lst:
if i in filename:
filepath = os.path.join(rootdir, filename)
newfilepath = os.path.join(rootdir, filename.replace(i, ""))
os.rename(filepath, newfilepath)
#No need to loop through other possibilities if we've found a match
break
for i in lst2:
if i in filename:
filepath = os.path.join(rootdir, filename)
newfilepath = os.path.join(rootdir, filename.replace(i, " "))
os.rename(filepath, newfilepath)
break
看起来不太漂亮,但应该可以!
此外,我是否可以建议您根据小写文件名字符串检查您的列表,以免不小心跳过字符串中某处带有大写字符的文件名?如果您不想让搜索区分大小写,这可能是个好主意!这是一个建议代码:
import os, string
rootdir = r'C:\Users\me\Downloads\Documents\Folder'
lst = ["this that", "This.that", "THIS.THAT", "this_that", "This.That"]
lst2 = ["_","__","___","."]
for filename in os.listdir(rootdir):
for i in lst:
if i.lower() in filename.lower():
filepath = os.path.join(rootdir, filename)
newfilepath = os.path.join(rootdir, filename.replace(i, ""))
os.rename(filepath, newfilepath)
#No need to loop through other possibilities if we've found a match
break
for i in lst2:
if i.lower() in filename.lower():
filepath = os.path.join(rootdir, filename)
newfilepath = os.path.join(rootdir, filename.replace(i, " "))
os.rename(filepath, newfilepath)
break
我有一段代码,其中大部分是我从这里的另一个问题中得到的。做了一些调整后,它似乎并没有工作。 目的很简单:我有一个包含很多文件的目录,所有文件都包含垃圾words/characters,需要用不同的东西替换。
我有 2 个包含这些词的列表。代码如下所示:
import os, string
rootdir = r'C:\Users\me\Downloads\Documents\Folder'
lst = ["this that", "This.that", "THIS.THAT", "this_that", "This.That"]
lst2 = ["_","__","___","."]
for filename in os.listdir(rootdir):
if any(i in filename for i in lst):
filepath = os.path.join(rootdir, filename)
newfilepath = os.path.join(rootdir, filename.replace(i, ""))
os.rename(filepath, newfilepath)
if any(i in filename for i in lst2):
filepath = os.path.join(rootdir, filename)
newfilepath = os.path.join(rootdir, filename.replace(i, " "))
os.rename(filepath, newfilepath)
但是当我 运行 时,错误是 NameError: name 'i' is not defined
不知道为什么会这样,我应该被定义为“出现在列表中的文件名的一部分”。至少我是这么认为的。尝试用不同的方式定义它,但没有成功。
有什么解决办法吗?谢谢
正如 han solo 和 Vaibhav 所指出的,变量 i 只存在于 for 循环的范围内。由于您使用单行 for 循环,因此无法在其余代码中访问该变量。我建议像这样使用多行 for 循环:
import os, string
rootdir = r'C:\Users\me\Downloads\Documents\Folder'
lst = ["this that", "This.that", "THIS.THAT", "this_that", "This.That"]
lst2 = ["_","__","___","."]
for filename in os.listdir(rootdir):
for i in lst:
if i in filename:
filepath = os.path.join(rootdir, filename)
newfilepath = os.path.join(rootdir, filename.replace(i, ""))
os.rename(filepath, newfilepath)
#No need to loop through other possibilities if we've found a match
break
for i in lst2:
if i in filename:
filepath = os.path.join(rootdir, filename)
newfilepath = os.path.join(rootdir, filename.replace(i, " "))
os.rename(filepath, newfilepath)
break
看起来不太漂亮,但应该可以!
此外,我是否可以建议您根据小写文件名字符串检查您的列表,以免不小心跳过字符串中某处带有大写字符的文件名?如果您不想让搜索区分大小写,这可能是个好主意!这是一个建议代码:
import os, string
rootdir = r'C:\Users\me\Downloads\Documents\Folder'
lst = ["this that", "This.that", "THIS.THAT", "this_that", "This.That"]
lst2 = ["_","__","___","."]
for filename in os.listdir(rootdir):
for i in lst:
if i.lower() in filename.lower():
filepath = os.path.join(rootdir, filename)
newfilepath = os.path.join(rootdir, filename.replace(i, ""))
os.rename(filepath, newfilepath)
#No need to loop through other possibilities if we've found a match
break
for i in lst2:
if i.lower() in filename.lower():
filepath = os.path.join(rootdir, filename)
newfilepath = os.path.join(rootdir, filename.replace(i, " "))
os.rename(filepath, newfilepath)
break