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

Greg gregbair at gmail.com
Sat Oct 2 19:04:53 CEST 2010


On Sat, Oct 2, 2010 at 11:56 AM, Josep M. Fontana <josep.m.fontana at gmail.com
> wrote:

> 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-02, 1501
> ...
> N-09, 1384
>
>
To process this, I'd check out the CSV (Comma Separated Values) module.

What I would like to do is to write a little script that would do the
> following:
>
> . Read each row of the text containing information about the centuries each
> one of the texts was written
> . Change the name of the file whose name starts with the code in the first
> column in the following way
>
>         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)
>
> I've found a lot of information about changing file names (so I know that I
> should be importing the os module), but none of the examples that were cited
> involved getting the information for the file changing operation from the
> contents of a document.


If it were me, I'd build a string using the information you want, then add
".txt" or whatever to the end of it, then use the os module to change the
filenames.

-- 
Greg Bair
gregbair at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101002/e2325557/attachment-0001.html>


More information about the Tutor mailing list