How to rename a file name using Python

Source: Internet
Author: User

ImportOS#Find FilesPath="c:\\users\\jw\\desktop\\python_work"#Os.listdir () method, listing all files#returns a list of the names of files or folders contained in the folder specified by Pathfiles=os.listdir (path)#Master Logic#for bulk operations, use a For loop forFinchFiles:#How to debug your code: Key places Print Statements to determine if this is a successful execution    Print(f)if "project20" inchF andF.endswith (". jpg"):        Print("The original file name is: {}". Format (f))#find the location where the old files are locatedold_file=Os.path.join (path,f)Print("old_file is {}". Format (old_file))#Specifies the location of the new file, and if this method is not used, the new filename is generated in the directory of this projectNew_file=os.path.join (Path,"project30.jpg")        Print("File'll be renamed as:{}". Format (new_file)) Os.rename (old_file,new_file)Print("the modified file name is: {}". Format (f))ImportOs,syspath="c:\\users\\jw\\desktop\\python_work"#switch to the corresponding directoryos.chdir (path)#List DirectoryPrint("Directory is:%s"%Os.listdir (OS.GETCWD ()))#removed fromOs.remove ("project30.jpg")#the directory after removalPrint("after removal:%s"%os.listdir (OS.GETCWD ()))


ImportOs

#查找文件
Path="C:\\Users\\Jw\\Desktop\\Python_work "
#os. Listdir () method, listing all files
#返回path指定的文件夹包含的文件或文件夹的名字的列表
Files=os.listdir (PATH)

#主逻辑
#对于批量的操作, use for loop
forFinchFiles
#调试代码的方法: Key places print Statements to determine if this is a successful execution
Print(f)
if"project20"inchF andF.endswith (". jpg"):
Print("The original file name is: {}". Format (f))

#找到老的文件所在的位置
Old_file=os.path.join (PATH,F)
Print("Old_file is {}". Format (Old_file))
#指定新文件的位置, if this method is not used, the new file name is generated in the directory of this project
New_file=os.path.join (Path,"Project30.jpg")
Print("File'll be renamed as:{}". Format (New_file))
Os.rename (Old_file,new_file)
Print("The modified file name is: {}". Format (f))




ImportOs,sys

Path="C:\\Users\\Jw\\Desktop\\Python_work "
# Switch to the "/var/www/html" directory
Os.chdir (PATH)

#列出目录
Print("Directory is:%s"%os.listdir (OS.GETCWD ()))

#移除
Os.remove ("Project30.jpg")

#移除后的目录
Print("After removal:%s"%os.listdir (OS.GETCWD ()))

How to rename a file name using Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.