Changing file names

T

Thomas Philips

I want to get a list of all subdirectories of a given directory, and
within each directory, change the name of each file using a
predetermined rule.

The function glob in module glob will give me a list of all the
subdirectories, and by looping over this list and repeatedly calling
glob, I can create lists of files within each directory, which are
easily edited to create lists of new filenames.

How then can I change the existing filenames to these new filenames?
What python functions should I be using, and what modules are they
contained in?

Thomas Philips
 
I

Irmen de Jong

Thomas said:
How then can I change the existing filenames to these new filenames?
What python functions should I be using, and what modules are they
contained in?

os.rename(oldfilename, newfilename)

--Irmen
 
F

Fredrik Lundh

Thomas said:
The function glob in module glob will give me a list of all the
subdirectories, and by looping over this list and repeatedly calling
glob, I can create lists of files within each directory, which are
easily edited to create lists of new filenames.

How then can I change the existing filenames to these new filenames?
What python functions should I be using, and what modules are they
contained in?

os.rename(oldname, newname)

found under "files and directories" in the library reference:

http://docs.python.org/lib/os-file-dir.html

</F>
 
B

beliavsky

I want to get a list of all subdirectories of a given directory, and
within each directory, change the name of each file using a
predetermined rule.

The function glob in module glob will give me a list of all the
subdirectories, and by looping over this list and repeatedly calling
glob, I can create lists of files within each directory, which are
easily edited to create lists of new filenames.

How then can I change the existing filenames to these new filenames?
What python functions should I be using, and what modules are they
contained in?

Thomas Philips

There is a "rename" function in the "os" module.
 
L

Larry Bates

Sounds like you want to use os.path.walk function
which recursively drills down through directories and
subdirectories. Then (as everyone else has pointed
out). You can use os.rename.

HTH,
Larry Bates
Syscon, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,047
Members
47,646
Latest member
xayaci5906

Latest Threads

Top