Python copy and rename many small csv files based on selected characters within the files
Question: I'm not a programmer; I'm a pilot who has done just a little bit of scripting
in a past life, so I'm completely non-current at this. I have searched the
forum and found somewhat similar problems that, with more expertise and time I
might be able to adapt to my problem, but I hope I can get closer by asking my
own question. I hope my problem is unique enough that those considering
answering do not feel their time is wasted, considering my disadvantage.
Anyway here is my problem:

Some of my crew members periodically have a need to rename a few hundred to
more than 1,000 small csv files based on a specific convention applied to
their contents. Not all of the files are used in a given project, but any
subset of them could be used, so automation makes a lot of sense here.
Currently this is done manually as needed. I can easily move all these files
into a single directory for processing, since all their file names are unique
as received.

Here are representative excerpts from two example csv files, preceded by their
respective file names (As I receive them):

* * *

A_13LSAT_2014-04-23_1431.csv:

    
    
    1,KDAL CURLO RW13L SAT 20140414_0644,SID,N/A,DDI
    2,*,RW13L(AER),SAT
    3,RW13L(AER),+325123.36,-0965121.20,RW31R(DER),+325031.35,-0965020.95
    4,1,1.2,+325123.36,-0965121.20,0.0,+325031.35,-0965020.95,2.0
    3,RW31R(DER),+325031.35,-0965020.95,GH13L,+324947.23,-0964929.84
    4,1,2.4,+325031.35,-0965020.95,0.0,+324947.23,-0964929.84,2.0
    5,TTT,0,0
    5,CVE,0,0
    

* * *

A_RROSEE_2014-04-03_1419.csv:

    
    
    1,KDFW SEEVR STAR RRONY SEEVR 20140403_1340,STAR,N/A,DDI
    2,*,RRONY,SEEVR
    3,RRONY,+333455.16,-0952530.56,ROWZE,+333233.02,-0954016.52
    4,1,12.6,+333455.16,-0952530.56,0.0,+333233.02,-0954016.52,2.0
    5,EIC,0,1
    5,SLR,0,0
    

* * *

I know these files are not code, but I entered them indented in this post so
they would display properly.

The files must be renamed due to the 8.3 limitation of the platform they are
used on. The convention is:

•On the first line, the first two characters in the second word of the second
"cell" (Which are the 6th and 7th characters of the second cell), and,

•on line 2, the first three characters of the third cell, and

•the first three characters of the fourth cell.

The contents and format of the files must remain unaltered. In theory this
convention yields unique names for every file so duplication of file names
should not be a problem.

The files above would be copied and renamed respectively to:

CURW1SAT.csv

SERROSEE.csv

That's it. Just a script that will scan a directory full of these csv files,
and create renamed copies in the same directory according the the convention I
just described, based on their contents. I'm attempting to use Activestate
Python 2.7.7.

Thanks in advance for any consideration.


Answer: It's not what you'd call pretty, but neither am I; and it works (and it's
simple)

    
    
    import os
    import glob
    
    fileset = set(glob.glob(os.path.basename(os.path.join(".", "*.csv"))))
    for filename in fileset:
        with open(filename, "r") as f:
            csv_file = f.readlines()
        out = csv_file[0].split(",")[1].split(" ")[1][:2]
        out += csv_file[1].split(",")[2][:3]
        out += csv_file[1].split(",")[3][:3]
        os.rename(filename, out + ".csv")
    

just drop this in the folder with all the csv's to be renamed and run it

