471,338 Members | 1,028 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Python thinks file is empty

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?

Nov 5 '07 #1
4 3725
On Nov 5, 9:19 am, loial <jldunn2...@googlemail.comwrote:
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

Nov 5 '07 #2
loial wrote:
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.
>>f = open('test.txt', 'w')
f.writelines('hello\n')
f.writelines('world\n')
f.close()
import shutil
shutil.copyfile('test.txt', 'test2.txt')
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

Nov 5 '07 #3
En Mon, 05 Nov 2007 12:57:15 -0300, Tim Chase
<py*********@tim.thechases.comescribió:
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...

--
Gabriel Genellina

Nov 5 '07 #4
loial <jl********@googlemail.comwrote:
>
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.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Nov 6 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Ian McConnell | last post: by
reply views Thread by Kurt B. Kaiser | last post: by
270 posts views Thread by Jordan | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.