[Tutor] Using contents of a document to change file names
joel.goldstick at gmail.com
Sat Oct 2 21:13:15 CEST 2010
On Sat, Oct 2, 2010 at 1:18 PM, Emile van Sebille <emile at fenx.com> wrote:
> On 10/2/2010 8:56 AM Josep M. Fontana said...
>> This is my first posting to this list. Perhaps this has a very easy answer
>> but before deciding to post this message I consulted a bunch of Python
>> manuals and on-line reference documents to no avail. I would be very
>> grateful if someone could lend me a hand with this.
> Hi Josep,
> Break the problem into pieces.
> Conceptually, you'll need to:
> -a- get the list of file names to change then for each
> -b- determine the new name
> -c- rename the file
> For -a- you'll need glob. For -c- use os.rename. -b- is a bit more
> involved. To break -b- down:
> -b1- break out the x-xx portion of the file name
> -b2- look up the corresponding year in the other file
> -b3- convert the year to the century-half structure
> -b4- put the pieces together to form the new file name
> For -b2- I'd suggest building a dictionary from your second files contents
> as a first step to facilitate the subsequent lookups.
> You haven't included any code and we can't gauge how far along you are, so
> follow up and post the code you write as you go and we'll be better able to
> help out.
> Hope this is enough to get you started,
> I echo Emile's excellent description. He didn't speak to changing what is
in the file. I would read from the first file a line at a time and
concatinate the citation date information to that string, then write the
string to a second file that is named according to your desired rule. Then
copy the rest of the text to the new file. When you are done, save the new
file and delete the original if you don't need it any longer.
> Here's the problem I want to solve. I have a lot of files with the
>> name structure:
>> These are different text documents that I want to process for an NLP
>> I'm starting. Each one of the texts belongs to a different century and it
>> important to be able to include the information about the century in the
>> name of the file as well as inside the text.
>> Then I have another text file containing information about the century
>> one of the texts was written. This document has the following structure:
>> A-01, 1278
>> A-02, 1501
>> N-09, 1384
>> What I would like to do is to write a little script that would do the
>> . Read each row of the text containing information about the centuries
>> 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
>> should be importing the os module), but none of the examples that were
>> involved getting the information for the file changing operation from the
>> contents of a document.
>> As you can imagine, I'm pretty green in Python programming and I was
>> the learn by doing method would work. I need to get on with this project,
>> though, and I'm kind of stuck. Any help you guys can give me will be very
>> Josep M.
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor