473,387 Members | 1,423 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,387 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 3903
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.