473,238 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 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 3878
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
6
by: Ian McConnell | last post by:
I've got a header file which lists a whole load of C functions of the form int func1(float *arr, int len, double arg1); int func2(float **arr, float *arr2, int len, double arg1, double arg2); ...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 421 open ( +3) / 3530 closed ( +8) / 3951 total (+11) Bugs : 963 open ( +4) / 6426 closed (+21) / 7389 total (+25) RFE : 255 open...
270
by: Jordan | last post by:
Hi everyone, I'm a big Python fan who used to be involved semi regularly in comp.lang.python (lots of lurking, occasional posting) but kind of trailed off a bit. I just wrote a frustration...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.