Rename with Counter using Python

import os
# Function to rename multiple files
def main():
   i = 1000
   path="C:/Users/user/Desktop/My Folder/New folder/New folder/"
   for filename in os.listdir(path):
      my_dest ="(" + str(i) + ")" + ".txt"
      my_source =path + filename
      my_dest =path + my_dest
      # rename() function will
      # rename all the files
      os.rename(my_source, my_dest)
      i += 1
# Driver Code
if __name__ == '__main__':
   # Calling main() function
   main()

OK, so I am trying to control the counter to only see txt files, if I have .txt, .jpeg, .mpeg,
everything gets rename, how can I control this to only .txt files

One more problem, when I use this Python counter or a batch counter it flips my file names

Example

File A.txt - this should be renamed to (1000).txt
File B.txt - this should be renamed to (1001).txt

Outcome

File B.txt - this should be renamed to (1000).txt
File A.txt - this should be renamed to (1001).txt

How can I control this from happening

Any advise will be much appreciated



from Recent Questions - Stack Overflow https://ift.tt/37DGOSb
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)