Difference between Popen and open() for reading a file

J

J

I was reading something from a code review a little while ago and saw
something that's got my curiosity up...

Say I had a file, foo.txt that I wanted to read from, only one time
and only read.

So what's the difference between this:

mylist = Popen(["cat","foo.txt"], stdout=PIPE).communicate()[0].splitlines()

and this:

f = open('foo.txt')
mylist = f.readlines()
f.close

Is there a reason why you would not use subprocess.Popen for this,
other than just not relying on external programs to perfrom the task?

what if that file only has one line in it, and that's all you're
interested in, and the file is guaranteed to only have that one line
it it?

For example:

say foo.txt contained only the number 123456789

what's wrong with doing this:

my_int = int(commands.getoutput('cat foo.txt')

or via the subprocess.Popen method mentioned above?
 
A

Aahz

Say I had a file, foo.txt that I wanted to read from, only one time
and only read.

So what's the difference between this:

mylist = Popen(["cat","foo.txt"], stdout=PIPE).communicate()[0].splitlines()

Is there a reason why you would not use subprocess.Popen for this,
other than just not relying on external programs to perfrom the task?

http://uuoc.com/
 

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
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top