Python thinks file is empty

L

loial

I am writing a file in python with writelines

f = open('/home/john/myfile',"w")
f.writelines("line1\n")
f.writelines("line2\n")
f.close()

But whenever I try to do anything with the file in python it finds no
data. I am trying ftp, copying the file...the resultant file is always
0 bytes, although if I look at the original file on the unix command
line, its fine

e.g.

shutil.copyfile('/home/john/myfile', '/home/john/myfile2')


Any ideas what the problem is?
 
K

kyosohma

I am writing a file in python with writelines

f = open('/home/john/myfile',"w")
f.writelines("line1\n")
f.writelines("line2\n")
f.close()

But whenever I try to do anything with the file in python it finds no
data. I am trying ftp, copying the file...the resultant file is always
0 bytes, although if I look at the original file on the unix command
line, its fine

e.g.

shutil.copyfile('/home/john/myfile', '/home/john/myfile2')

Any ideas what the problem is?

This works for me on Windows. I can open the file I create in notepad
and it has the correct text within it. I also tried using shutil to
copy it and that worked too.

How are you opening the file in Python? If you open it using the 'w'
flag, it'll just erase the file...

Mike
 
T

Tim Chase

loial said:
I am writing a file in python with writelines

f = open('/home/john/myfile',"w")
f.writelines("line1\n")
f.writelines("line2\n")
f.close()

But whenever I try to do anything with the file in python it finds no
data. I am trying ftp, copying the file...the resultant file is always
0 bytes, although if I look at the original file on the unix command
line, its fine

e.g.

shutil.copyfile('/home/john/myfile', '/home/john/myfile2')

Any ideas what the problem is?

A failure to copy/paste the lines as you had them? Or perhaps
accessing the file before it's been closed/flushed? Or perhaps
you're opening it in write-mode after you've closed the file
(thus overwriting your content with a blank file)?

As shown in the below session, it works for me:

####################################
tchase@agix2:~/tmp$ python
Python 2.4.4 (#2, Apr 5 2007, 20:11:18)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.tchase@agix2:~/tmp$ cat test.txt
hello
world
tchase@agix2:~/tmp$ cat test2.txt
hello
world
#####################################

You may also want to use f.write() rather than f.writelines()
which seems to be your usage. f.writelines() is used for writing
an iterable of strings. Thus, it would be used something like
this trivial example:

f.writelines(['hello\n', 'world\n'])

To confuse matters, it happens to work in your example, because a
string is an iterable that returns each character in that string
as the result, so code like this

f.writelines('hello\n')

is effectively doing something like this

f.write('h')
f.write('e')
f.write('l')
f.write('l')
f.write('o')
f.write('\n')

(unless writelines is optimized to smarten up the code here)

-tkc
 
G

Gabriel Genellina

En Mon, 05 Nov 2007 12:57:15 -0300, Tim Chase
To confuse matters, it happens to work in your example, because a
string is an iterable that returns each character in that string
as the result, so code like this

f.writelines('hello\n')

is effectively doing something like this

f.write('h')
f.write('e')
f.write('l')
f.write('l')
f.write('o')
f.write('\n')

(unless writelines is optimized to smarten up the code here)

As of 2.5.1, it's not :(
writelines is perhaps one of the worst used functions in Python. It'b been
there for ages (since 1.5.2 at least) but a lot of people does not know it
even exists (and roll their own, *always* slower; map(f.write, lines)
appears to be the favorite spelling...) or, if they are aware of it, use
it wrongly as you pointed out above.
Perhaps some of them come from Delphi/Pascal, looking for something
similar to writeln, and writelines is the closest match...
 
T

Tim Roberts

loial said:
I am writing a file in python with writelines

f = open('/home/john/myfile',"w")
f.writelines("line1\n")
f.writelines("line2\n")
f.close()

Are you absolutely sure it looks like that? If you wrote this:

f.close

(as reformed VB programmers tend to write) it would run without error or
warning, but the file would not actually be closed. The data would be hung
up the buffer, and the file would be 0 length.
 

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,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top