#importing the os library
import os
#Defining a function called rename_files()
def rename_files():
#**get file names from a folder**
file_list = os.listdir(r"C:\Users\user\Desktop\webdev\prank")
#Knowing about current working directory
saved_path = os.getcwd()
print("Current Working Directory is " +saved_path)
#changing the directory
os.chdir(r"C:\Users\user\Desktop\webdev\prank")
#**for each file, rename filename**
#using For loop to change the name for each file name
for file_name in file_list:
os.rename(file_name,file_name.translate(None,"0123456789")
#saving back to current working directory
os.chdir(saved_path)
#calling the function
rename_files()
我收到此错误:
os.chdir(saved_path)处的无效错误
我正在使用Python 3.6。
答案 0 :(得分:0)
我认为您缺少右括号。
#importing the os library
import os
#Defining a function called rename_files()
def rename_files():
#**get file names from a folder**
file_list = os.listdir(r"C:\Users\user\Desktop\webdev\prank")
#Knowing about current working directory
saved_path = os.getcwd()
print("Current Working Directory is " +saved_path)
#changing the directory
os.chdir(r"C:\Users\user\Desktop\webdev\prank")
#**for each file, rename filename**
#using For loop to change the name for each file name
for file_name in file_list:
os.rename(file_name,file_name.translate(None,"0123456789") ) <<<<<== Here
#saving back to current working directory
os.chdir(saved_path)
#calling the function
rename_files()