[Tutor] Using contents of a document to change file names

Alan Gauld alan.gauld at btinternet.com
Sun Oct 3 02:17:30 CEST 2010

"Josep M. Fontana" <josep.m.fontana at gmail.com> wrote

> Here's the problem I want to solve. I have a lot of files with the 
> following
> name structure:
> A-01-namex.txt

> Then I have another text file containing information about the 
> century each
> one of the texts was written. This document has the following 
> structure:
> A-01, 1278
>        A-01-namex.txt --> A-01-namex_13-2.txt
>    Where 13-1 means: 13th 2nd half. Obviously this information would 
> com
> from the second column in the text: 1278 (the first two digits + 1 =
> century; if the 3rd and 4th digits > 50, then 2; if < 50 then     1)
> Then in the same script or in a new one, I would need to open each 
> one of
> the texts and add information about the century they were written on 
> the
> first line preceded by some symbol (e.g @13-2)

If you break it into discrete steps it is all petty basic file and 
string processing.

You can rename the file using the os module
You can construct the filename using string methods
You can get the data to put in the filename by reading a file
You can add the new century data by writing a file.

Now tell us which of those operations is giving you the headache?
Or is it the issue of building the overall program structure?

Once we know more about where your specific problem lies we can give
you a better answer.

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list