我希望完成的是扫描指定目录中的所有文件并删除名称中包含的数字。问题是我无法使用下面的代码更改目录。
我目前正处于Udacity Full Stack Nanodegree计划中,所以如果有人可以帮助我,那么该计划也将是一个优势。
以下是代码:
import os
def rename_files():
#(1) Get file names from a folder
file_list = os.listdir("/Users/bill/Documents/web/LocalServer/prank")
#print (file_list)
saved_path = os.getcwd()
print("Our current working directory is " + saved_path)
os.chdir('/Users/bill/Documents/web/LocalServer/prank')
print("Our current working directory is ", saved_path)
#(2) For each file, rename filename
for file_name in file_list:
#print("Old Name - " + file_name)
#print("New Name - " + file_name.translate("0123457689"))
os.rename(file_name, file_name.translate("0123457689"))
os.chdir(saved_path)
print("Our current working directory is ", saved_path)
rename_files()
以下是我得到的输出:
Our current working directory is /Users/bill/Documents/web/LocalServer
Our current working directory is /Users/bill/Documents/web/LocalServer
Our current working directory is /Users/bill/Documents/web/LocalServer
更新1:
我终于更改了目录,但我仍然无法重命名文件。 (例如:68chicago.jpg到chicago.jpg)
这是我目前的代码:
import os
def rename_files():
#(1) Get file names from a folder
file_list = os.listdir("/Users/bill/Documents/web/LocalServer/prank")
#print (file_list)
saved_path = os.getcwd()
print("Our current working directory is " + saved_path)
os.chdir('/Users/bill/Documents/web/LocalServer/prank')
new_path = os.getcwd()
print("Our current working directory is ", new_path)
#(2) For each file, rename filename
for file_name in file_list:
#print("Old Name - " + file_name)
#print("New Name - " + file_name.translate("0123457689"))
os.rename(file_name, file_name.translate("0123457689"))
print("Our current working directory is ", new_path)
rename_files()
这是我目前的输出:
Our current working directory is /Users/bill/Documents/web/LocalServer
Our current working directory is /Users/bill/Documents/web/LocalServer/prank
Our current working directory is /Users/bill/Documents/web/LocalServer/prank
更新2:
由于@Dan,我终于解决了这个问题。
这是他的代码:
import os
def rename_files():
#(1) Get file names from a folder
file_list = os.listdir("/Users/bill/Documents/web/LocalServer/prank")
#print (file_list)
saved_path = os.getcwd()
print("Our current working directory is " + saved_path)
os.chdir('/Users/bill/Documents/web/LocalServer/prank')
new_path = os.getcwd()
print("Our current working directory is ", new_path)
#(2) For each file, rename filename
for file_name in file_list:
#print("Old Name - " + file_name)
#print("New Name - " + file_name.translate("0123457689"))
os.rename(file_name, ''.join([i for i in file_name if not i.isdigit()])) # This works on my machine
os.chdir(saved_path)
print("Our current working directory is ", saved_path)
rename_files()
答案 0 :(得分:3)
看起来您想要更改为dir然后更改回第一个目录?如果是这种情况,你想要这样的东西:
import os
def rename_files():
#(1) Get file names from a folder
file_list = os.listdir("/Users/bill/Documents/web/LocalServer/prank")
#print (file_list)
saved_path = os.getcwd()
print("Our current working directory is " + saved_path)
os.chdir('/Users/bill/Documents/web/LocalServer/prank')
new_path = os.getcwd()
print("Our current working directory is ", new_path)
#(2) For each file, rename filename
for file_name in file_list:
#print("Old Name - " + file_name)
#print("New Name - " + file_name.translate("0123457689"))
os.rename(file_name, ''.join([i for i in file_name if not i.isdigit()])) # This works on my machine
os.chdir(saved_path)
print("Our current working directory is ", saved_path)
rename_files()
答案 1 :(得分:1)
您的问题是您错误地使用了.translate()
。它根本不会更改文件名,因此重命名实际上将X重命名为X:
'68chicago.jpg'.translate('0123457689')
# '68chicago.jpg'
尝试使用列表理解:
def clean_name(name):
return ''.join(x for x in name if not x.isdigit())
# 'chicago.jpg'
答案 2 :(得分:1)
主要问题是您的代码没有以正确的方式使用str.translate()
方法 - 您需要将转码表传递给它才能正常工作。
以下内容适用于避免重命名不需要的文件(即不将其重命名为当前名称)。这需要一些额外的处理,但执行无用的操作系统级操作可能更快。
注意:更强大的解决方案还需要处理os.rename()
调用可能引发的任何异常 - 这肯定会因多种原因而发生。
例如:
代码:
from contextlib import contextmanager
import string
import os
@contextmanager
def temp_cd(path):
"""Temporarily change the current directory to path, yield, then restore it."""
saved_path = os.getcwd()
yield
os.chdir(saved_path)
def rename_files(path):
TRANS_TABLE = str.maketrans(dict.fromkeys(string.digits))
with temp_cd(path):
for file_name in os.listdir(path):
if any(ch in file_name for ch in string.digits):
# os.rename(file_name, file_name.translate(TRANS_TABLE))
print(file_name, '->', file_name.translate(TRANS_TABLE))
rename_files("/Users/bill/Documents/web/LocalServer/prank")
答案 3 :(得分:1)
这是一个不需要更改当前目录的版本。只需指定要重命名文件的目录。如果指定旧名称和新名称的完整路径,则不需要更改目录。
这也说明了如何正确使用translate
。 maketrans
需要一个,两个或三个参数(请参阅docs)。三参数版本采用两个相等长度的字符串进行1:1翻译,另外还有第三个要删除的字符参数。它返回一个适合与translate
一起使用的字典。
import os
def rename_files(directory):
xlat = str.maketrans('','','0123457689')
file_list = os.listdir(directory)
for file_name in file_list:
old_name = os.path.join(directory,file_name)
new_name = os.path.join(directory,file_name.translate(xlat))
os.rename(old_name,new_name)
rename_files('/Users/bill/Documents/web/LocalServer/prank')