reading from list with paths

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • antar2

    reading from list with paths

    Hello,

    I would like to read and print files, of which the complete filepaths
    are
    mentioned in another textfile. In this textfile (list.txt) are for
    example the following paths:

    /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_1LG_f 01.TextGrid
    /data/chorec/chorec-nieuw/s01/S01C001M1/
    S01C001M1_1LGPs eudo_f01.TextGr id
    /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_AVI1_ f01.TextGrid

    I know how to open and read one file in my current directory,
    but after trying to find this out my self, I give up...

    I already got one answer for this question, but it did not work

    Thanks a lot

    Antar2

  • =?iso-8859-1?q?C=E9dric_Lucantis?=

    #2
    Re: reading from list with paths

    Le Wednesday 25 June 2008 20:59:38 antar2, vous avez écrit :
    Hello,
    >
    I would like to read and print files, of which the complete filepaths
    are
    mentioned in another textfile. In this textfile (list.txt) are for
    example the following paths:
    >
    /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_1LG_f 01.TextGrid
    /data/chorec/chorec-nieuw/s01/S01C001M1/
    S01C001M1_1LGPs eudo_f01.TextGr id
    /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_AVI1_ f01.TextGrid
    >
    I know how to open and read one file in my current directory,
    but after trying to find this out my self, I give up...
    >
    I already got one answer for this question, but it did not work
    >
    What's the problem exactly ? If you already know how to read a file you have
    all what you need:

    f_list = open('list.txt' )
    for filename in f_list :
    f = open(filename)
    print f.read()
    f.close()
    f_list.close()

    If you get an error, please post the full error message with the backtrace.

    --
    Cédric Lucantis

    Comment

    • bruno.desthuilliers@gmail.com

      #3
      Re: reading from list with paths

      On 25 juin, 20:59, antar2 <desoth...@yaho o.comwrote:
      Hello,
      >
      (snip repost of the very same question)
      I already got one answer for this question, but it did not work
      For which definition of "did not work" ? How is your code ? What did
      you expect, and what did you get ? Sorry to ask these questions, but
      my crystal ball is currently down for maintainance...





      Comment

      • Lie

        #4
        Re: reading from list with paths

        On Jun 26, 1:59 am, antar2 <desoth...@yaho o.comwrote:
        Hello,
        >
        I would like to  read and print files, of which the complete filepaths
        are
         mentioned in another textfile. In this textfile (list.txt)  are for
         example the following paths:
        >
        /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_1LG_f 01.TextGrid
         /data/chorec/chorec-nieuw/s01/S01C001M1/
         S01C001M1_1LGPs eudo_f01.TextGr id
         /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_AVI1_ f01.TextGrid
        >
        I know how to open and read one file in my current directory,
         but after trying to find this out my self, I give up...
        >
        Is this Window's path or Unix-like's path. In Windows, path is
        generally like this:
        "folder\\data.t xt" OR r"folder\data.t xt"
        because Windows use backslash as its path separator

        In Unix-like OS (Unix, Linux, OSX), they use forward slash.

        In general, Windows (Explorer) doesn't care whether you used backslash
        or forward slash, but I'm not sure if we can use forwardslash in
        Python.

        I already got one answer for this question, but it did not work
        It didn't work... how? Any error message? or what's the content of the
        name that should be file object.
        >
        Thanks a lot
        >
        Antar2

        Comment

        • norseman

          #5
          Re: reading from list with paths


          antar2 wrote:
          Hello,
          >
          I would like to read and print files, of which the complete filepaths
          are
          mentioned in another textfile. In this textfile (list.txt) are for
          example the following paths:
          >
          /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_1LG_f 01.TextGrid
          /data/chorec/chorec-nieuw/s01/S01C001M1/
          S01C001M1_1LGPs eudo_f01.TextGr id
          /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_AVI1_ f01.TextGrid
          >
          I know how to open and read one file in my current directory,
          but after trying to find this out my self, I give up...
          >
          I already got one answer for this question, but it did not work
          >
          Thanks a lot
          >
          Antar2
          >
          --

          >
          >
          >
          ---------------------------------
          I think what you want is more like:

          contents of a file named printthem.py
          ......
          import os

          p = open('list.txt' )
          for vpath in p:
          for f in os.listdir(vpat h):
          print '\n\tContents of:',vpath+'/'+f
          f = open(vpath+'/'+f)
          print f.read()
          f.close()
          print '\n\t\t\t\tEND OF FILE\x0C'
          p.close()
          print "All Done. Is there any paper left?"
          ......

          usage: python printthem.py >/dev/lp0 Unix
          python printthem.py prn: Microsoft

          On Microsoft use '\\' in place of '/' following each vpath above
          The \x0C is Ctrl-L aka: ff or FormFeed


          norseman

          Comment

          • MRAB

            #6
            Re: reading from list with paths

            On Jun 25, 10:00 pm, norseman <norse...@hughe s.netwrote:
            antar2 wrote:
            Hello,
            >
            I would like to  read and print files, of which the complete filepaths
            are
             mentioned in another textfile. In this textfile (list.txt)  are for
             example the following paths:
            >
            /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_1LG_f 01.TextGrid
             /data/chorec/chorec-nieuw/s01/S01C001M1/
             S01C001M1_1LGPs eudo_f01.TextGr id
             /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_AVI1_ f01.TextGrid
            >
            I know how to open and read one file in my current directory,
             but after trying to find this out my self, I give up...
            >
            I already got one answer for this question, but it did not work
            >
            Thanks a lot
            >
            Antar2
            >>
            ---------------------------------
            I think what you want is more like:
            >
            contents of a file named printthem.py
            .....
            import os
            >
            p = open('list.txt' )
            for vpath in p:
               for f in os.listdir(vpat h):
                 print '\n\tContents of:',vpath+'/'+f
                 f = open(vpath+'/'+f)
                 print f.read()
                 f.close()
                 print '\n\t\t\t\tEND OF FILE\x0C'
            p.close()
            print "All Done. Is there any paper left?"
            .....
            >
            usage: python printthem.py >/dev/lp0         Unix
                    python printthem.py prn:           Microsoft
            >
            On Microsoft use '\\' in place of '/' following each vpath above
            The \x0C is Ctrl-L   aka: ff or FormFeed
            >
            norseman
            FYI, Python also understands '\f' ('\x0c').

            Interestingly, print repr('\a\b\f\n\ r\t\v') gives '\x07\x08\x0c\n \r\t
            \x0b'. Is that because '\n', '\r' and '\t' occur so often that it was
            decided that they should be treated specially?

            Comment

            Working...