Python batch modifies file names while writing files to txt file __python

Source: Internet
Author: User
Batch modification file name
import os

dir = os.getcwd ()
dir = Os.chdir (R ' C:\Users\Administrator\Desktop\cupdata ')
j = 0 for
file in Os.listdir (dir):
     #os. Listdir ('. ') Traverses each file name within the folder and returns a list if file[-2 containing the file name
    :] = = ' py ':
        continue   #过滤掉改名的. py file
    os.rename (File,str (j) + '. JPG ')
    j+=1 
    Print (file) #
print (dir)
# # dir = ' c:\windows '
# # if Os.path.exists (dir):
# #     print ("Hello World") # #
Else: #     print ("Hello World1")  
Modify file name while writing file name to TXT file
import os

dir = os.getcwd ()
dir = Os.chdir (R ' C:\Users\Administrator\Desktop \img ')
f = open (R ' C:\Users\Administrator\Desktop\bg.txt ', ' a ')
j = 0 for
file in Os.listdir (dir):
     #os. Listdir ('. ') Traverses each file name within the folder and returns a list if file[-2 containing the file name
    :] = = ' py ':
        continue   #过滤掉改名的. py file
    os.rename (file, ' BG ' + Str (j) + '. jpg ')
    f.write (' Img/bg ' +str (j) + '. jpg ' + ' \ n ')
    j+=1 
    print (file)
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.