By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,369 Members | 1,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,369 IT Pros & Developers. It's quick & easy.

reading from list with paths

P: n/a
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

Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies

P: n/a
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.

--
Cédric Lucantis
Jun 27 '08 #2

P: n/a
On 25 juin, 20:59, antar2 <desoth...@yahoo.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...

Jun 27 '08 #3

P: n/a
Lie
On Jun 26, 1:59*am, antar2 <desoth...@yahoo.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_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.
>
Thanks a lot

Antar2
Jun 27 '08 #4

P: n/a

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_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

--
http://mail.python.org/mailman/listinfo/python-list
---------------------------------
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
Jun 27 '08 #5

P: n/a
On Jun 25, 10:00*pm, norseman <norse...@hughes.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_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
--
http://mail.python.org/mailman/listinfo/python-list

---------------------------------
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?
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.