-1

I've beem browsing through some older questions and looking for a way to rename files that load into a folder.

So far I've got a folder which gets loaded with files some times per day a few files.

First of all I want to move all these files from lets say 'C:\Folder' to 'F:\Folder1'.

After I've moved the files I want to rename them using a python script. So far I've been looking at something like the below.

import os, sys
print "Current directory is: %s" %os.getcwd()

# listing directories
print "The dir is: %s"%os.listdir(os.getcwd())

# renaming file "aa1.txt"
os.renames("aa1.txt","newdir/aanew.txt")

print "Successfully renamed."

But I want to loop it so that all files get their names changed. Example MyFile1.csv if there are more MyFile2, MyFile3 etc

Thanks!

SterlinkArcher
  • 673
  • 3
  • 21
  • 36

1 Answers1

1

you might want to reference the answers to this question: How to list all files of a directory? on stackoverflow. In a nutshell, os.listdir or walk will do the trick to get you a list of file names. Then, you can rename them at will.

Three snippets from the referenced document: How to list all files of a directory?

os.listdir("somedirectory")

filenames = next(os.walk(path))[2]

# Return everything under C:\Users\admin that contains a folder called wlp.
from glob import glob
glob('C:\Users\admin\*\wlp')

instead of simply polling a dir, here are some tools to check if a windows dir has modifications you may want to act upon:

I'm mostly a linux user, so for any linux users out there, you may also want to look at inotify (which, despite some other comments I've read about it, worked just fine for me):

I hope that helps.

Community
  • 1
  • 1
KurtB
  • 604
  • 7
  • 8