<div class="gmail_quote"><div>Hi,</div><div><br></div><div>First let me apologize for taking so long to acknowledge your answers and to thank you (Eduardo, Peter, Greg, Emile, Joel and Alan, sorry if I left anyone) for your help and your time. </div>
<div><br></div><div>One of the reasons I took so long in responding (besides having gotten busy with some urgent matters related to my work) is that I was a bit embarrassed at realizing how poorly I had defined my problem. </div>
<div>As Alan said, I should at least have told you which operations were giving me a headache. So I went back to my Python reference books to try to write some code and thus be able to define my problems more precisely. Only after I did that, I said to myself, I would come back to the list with more specific questions.</div>
<div><br></div><div>The only problem is that doing this made me painfully aware of how little Python I know. Well, actually my problem is not so much that I don&#39;t know Python as that I have very little experience programming in general. Some years ago I learned a little Perl and basically I used it to do some text manipulation using regular expressions but that&#39;s all my experience. In order to learn Python, I read a book called &quot;Beginning Python: From Novice to Professional&quot; and I was hoping that just by starting to use the knowledge I had supposedly acquired by reading that book to solve real problems related to my project I would learn. But this turned out to be much more difficult than I had expected. Perhaps if I had worked through the excellent book/tutorial Alan has written (of which I was not aware when I started), I would be better prepared to confront this problem.</div>
<div><br></div><div>Anyway (sorry for the long intro), since Emile laid out the problem very clearly, I will use his outline to point out the problems I&#39;m having:</div><div><br></div><div>Emile says:</div><div>--------------</div>
<div><div>Conceptually, you&#39;ll need to:</div><div><br></div><div>  -a- get the list of file names to change then for each</div><div>  -b- determine the new name</div><div>  -c- rename the file</div><div><br></div><div>
For -a- you&#39;ll need glob. For -c- use os.rename.  -b- is a bit more</div><div>involved.  To break -b- down:</div><div><br></div><div>  -b1- break out the x-xx portion of the file name</div><div>  -b2- look up the corresponding year in the other file</div>
<div>  -b3- convert the year to the century-half structure</div><div>  -b4- put the pieces together to form the new file name</div><div><br></div><div>For -b2- I&#39;d suggest building a dictionary from your second files</div>
<div>contents as a first step to facilitate the subsequent lookups.</div></div><div><br></div><div>---------------------</div><div><br></div><div>OK. Let&#39;s start with -b- . My first problem is that I don&#39;t really know how to go about building a dictionary from the file with the comma separated values. I&#39;ve discovered that if I use a file method called &#39;readlines&#39; I can create a list whose elements would be each of the lines contained in the document with all the codes followed by comma followed by the year. Thus if I do:</div>
<div>
<p class="p1"><span class="s1">fileNameCentury = open(r</span>&#39;/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt&#39;<span class="s1">).readlines() </span></p><p class="p1">Where &#39;FileNamesYears.txt&#39; is the document with the following info:</p>
<p class="p1">A-01, 1278<br>A-02, 1501<br>...<br>N-09, 1384</p><p class="p1">I get a list of the form [&#39;A-01,1374\rA-02,1499\rA-05,1449\rA-06,1374\rA-09, ...]</p><p class="p1">Would this be a good first step to creating a dictionary? It seems to me that I should be able to iterate over this list in some way and make the substring before the comma the key and the substring after the comma its value. The problem is that I don&#39;t know how. Reading the book I read has not prepared me for this. I have the feeling that all the pieces of knowledge I need to solve the problem where there, but I don&#39;t know how to put them together. Greg mentioned the csv module. I checked the references but I could not see any way in which I could create a dictionary using that module, either. </p>
<p class="p1">Once I have the dictionary built, what I would have to do is use the os module (or would it be the glob module?) to get a list of the file names I want to change and build another loop that would iterate over those file names and, if the first part of the name (possibly represented by a regular expression of the form r&#39;[A-Z]-[0-9]+&#39;) matches one of the keys in the dictionary, then a) it would get the value for that key, b) would do the numerical calculation to determine whether it is the first part of the century or the second part and c) would insert the string representing this result right before the extension .txt.   </p>
<p class="p1">In the abstract it sounds easy, but I don&#39;t even know how to start.  Doing some testing with glob I see that it returns a list of strings representing the whole paths to all the files whose names I want to manipulate. But in the reference documents that I have consulted, I see no way to change those names. How do I go about inserting the information about the century right before the substring &#39;.txt&#39;?</p>
<p class="p1">As you see, I am very green. My embarrassment at realizing how basic my problems were made me delay writing another message but I decided that if I don&#39;t do it, I will never learn. </p><p class="p1">Again, thanks so much for all your help. </p>
<p class="p1">Josep M.</p><p class="p1"> </p></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Message: 2<br>
Date: Sat, 2 Oct 2010 17:56:53 +0200<br>
From: &quot;Josep M. Fontana&quot; &lt;<a href="mailto:josep.m.fontana@gmail.com">josep.m.fontana@gmail.com</a>&gt;<br>
To: <a href="mailto:tutor@python.org">tutor@python.org</a><br>
Subject: [Tutor] Using contents of a document to change file names<br>
Message-ID:<br>
        &lt;AANLkTikjOFYhieL70E=-<a href="mailto:BaE_PEdc0nG%2BiGY3j%2BqO%2BFMZ@mail.gmail.com">BaE_PEdc0nG+iGY3j+qO+FMZ@mail.gmail.com</a>&gt;<br>
Content-Type: text/plain; charset=&quot;iso-8859-1&quot;<br>
<br>
Hi,<br>
<br>
This is my first posting to this list. Perhaps this has a very easy answer<br>
but before deciding to post this message I consulted a bunch of Python<br>
manuals and on-line reference documents to no avail. I would be very<br>
grateful if someone could lend me a hand with this.<br>
<br>
Here&#39;s the problem I want to solve. I have a lot of files with the following<br>
name structure:<br>
<br>
A-01-namex.txt<br>
A-02-namey.txt<br>
...<br>
N-09-namez.txt<br>
<br>
These are different text documents that I want to process for an NLP project<br>
I&#39;m starting. Each one of the texts belongs to a different century and it is<br>
important to be able to include the information about the century in the<br>
name of the file as well as inside the text.<br>
<br>
Then I have another text file containing information about the century each<br>
one of the texts was written. This document has the following structure:<br>
<br>
A-01, 1278<br>
A-02, 1501<br>
...<br>
N-09, 1384<br>
<br>
What I would like to do is to write a little script that would do the<br>
following:<br>
<br>
. Read each row of the text containing information about the centuries each<br>
one of the texts was written<br>
. Change the name of the file whose name starts with the code in the first<br>
column in the following way<br>
<br>
        A-01-namex.txt --&gt; A-01-namex_13-2.txt<br>
<br>
    Where 13-1 means: 13th 2nd half. Obviously this information would com<br>
from the second column in the text: 1278 (the first two digits + 1 =<br>
century; if the 3rd and 4th digits &gt; 50, then 2; if &lt; 50 then     1)<br>
<br>
Then in the same script or in a new one, I would need to open each one of<br>
the texts and add information about the century they were written on the<br>
first line preceded by some symbol (e.g @13-2)<br>
<br>
I&#39;ve found a lot of information about changing file names (so I know that I<br>
should be importing the os module), but none of the examples that were cited<br>
involved getting the information for the file changing operation from the<br>
contents of a document.<br>
<br>
As you can imagine, I&#39;m pretty green in Python programming and I was hoping<br>
the learn by doing method would work.  I need to get on with this project,<br>
though, and I&#39;m kind of stuck. Any help you guys can give me will be very<br>
helpful.<br>
<br>
Josep M.<br><br><br>
</blockquote></div><br>