reading from list with paths

A

antar2

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_f01.TextGrid
/data/chorec/chorec-nieuw/s01/S01C001M1/
S01C001M1_1LGPseudo_f01.TextGrid
/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
 
C

Cédric Lucantis

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_f01.TextGrid
/data/chorec/chorec-nieuw/s01/S01C001M1/
S01C001M1_1LGPseudo_f01.TextGrid
/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.
 
B

bruno.desthuilliers

(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...
 
L

Lie

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_f01.TextGrid
 /data/chorec/chorec-nieuw/s01/S01C001M1/
 S01C001M1_1LGPseudo_f01.TextGrid
 /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.txt" OR r"folder\data.txt"
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.
 
N

norseman

antar2 said:
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_f01.TextGrid
/data/chorec/chorec-nieuw/s01/S01C001M1/
S01C001M1_1LGPseudo_f01.TextGrid
/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(vpath):
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
 
M

MRAB

---------------------------------
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(vpath):
     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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top