Join us at IRC!
It is never to LATE to become what you never WERE.
Friday, May 25, 2012
Navigation
Members Online
Total Online: 36
Web Spiders: 15
Guests Online: 35
Members Online: 1

Registered Members: 70209
Newest Member: KalareShou
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Python: file write issue

FallFromINFINITY
Member

Posts: 9
Location:
Joined: 23.03.09
Rank:
Apprentice
Posted on 15-12-09 10:07
ok, here's the problem. I have a small program that needs to read from a file store it in memory a list, edit the list, then rewrite to the same file.

file example.txt
"this", "is", "only", "an", "example"


file code.py
with open("c:/example.txt") as list:
for line in list:
items = line
items2 = items + items
list.write(items2)
**note: <tab> is an actual tab. it is not printed in the file. i'm using it to show that i'm not closing the function

this should make the file example.txt as such:
"this", "is", "only", "an", "example", "this", "is", "only", "an", "example"


but i receive, for the line "list.write(item2)"
IOError: Errno9 bad file descblockedriptor

what's wrong? if i add the line "print items" or "print items2" it outputs the correct list data. it just errors when i try to write the data. I couldn't see anything in the manual.
If it helps, i have python 2.6 on a 64bit windows 7


--------------------------------------------------------------------
Who is "General Error" and why is he reading my harddisk?

Edited by ynori7 on 15-12-09 13:54
FallFromINFINITY@hotmail.com FallFromINFINITY
Author

RE: Python: file write issue

ynori7
Future Emperor of Earth



Posts: 1481
Location: #valhalla
Joined: 08.10.07
Rank:
Diabolical
Posted on 15-12-09 15:52
It says you have a bad file descblockedriptor because you didn't specify a mode when opening the file. If you don't specify, it defaults to 'r' (read). Try this:
with open("c:/example.txt", 'r+') as list:

That should allow both read and write.




ynori7 http://halls-of-valhalla.org
Author

RE: Python: file write issue

stealth-
Member



Posts: 999
Location: Eh?
Joined: 10.04.09
Rank:
God
Posted on 15-12-09 16:00
I believe it's also better coding etiquette (would that be the word?) to open the file in read mode, read from it, close it, open in write mode (w), and then write to it and close it again.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealth-x.com
http://www.stealth-x.com
Author

RE: Python: file write issue

reaper4334
Member



Posts: 314
Location: Uk
Joined: 24.11.06
Rank:
God
Posted on 15-12-09 17:44
stealth- wrote:
I believe it's also better coding etiquette (would that be the word?) to open the file in read mode, read from it, close it, open in write mode (w), and then write to it and close it again.


I agree, that way if you encounter any permission errors or anything, you might not have as large a problem.






reaper4334@hotmail.co.uk http://reaper4334.freehostia.com
Author

RE: Python: file write issue

ynori7
Future Emperor of Earth



Posts: 1481
Location: #valhalla
Joined: 08.10.07
Rank:
Diabolical
Posted on 15-12-09 17:51
stealth- wrote:
I believe it's also better coding etiquette (would that be the word?) to open the file in read mode, read from it, close it, open in write mode (w), and then write to it and close it again.

Indeed. I just gave the solution that involved the least change to the OP's code. He needs to write his code himself.




ynori7 http://halls-of-valhalla.org
Author

RE: Python: file write issue

FallFromINFINITY
Member

Posts: 9
Location:
Joined: 23.03.09
Rank:
Apprentice
Posted on 15-12-09 20:34
thanks. that works now.

I just noticed another, new problem. It reads the entire file as a string, not a list of several strings.

Would it be possible to import a file like:
example.py
items = ("this", "is", "an", "example")


so that it would place the list into memory, and then write to the file adding the new data, so it turns out like:
items = ("this", "is", "an", "example", "this", "is", "an", "example")



--------------------------------------------------------------------
Who is "General Error" and why is he reading my harddisk?
FallFromINFINITY@hotmail.com FallFromINFINITY
Author

RE: Python: file write issue

wolfmankurd
Member



Posts: 1519
Location: UK
Joined: 30.05.05
Rank:
God
Posted on 16-12-09 01:18
whats the basis of splitting them?
readlines splits the file into lines.
readline reads one(?) line.


so given a file, text_file, as
ali
monkey
cheese
house

lines.readline()
gives ali (and monkey next time etc etc)
and lines.readlines()
gives
["ali", "monkey", "cheese", "house"]

assuming "lines" is an open file handle to text_file

to split at ' ' the whole file then do

lines.read().split()

this reads it as large string then splits it at spaces (the default) if you want to split at b's then you would use split('b')



BY READING MY POST, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE USE OF THIS (MIS)INFORMATION.




Edited by wolfmankurd on 16-12-09 13:23
Widowmakr@hotmail.com http://LetsHackStuff.com
Author

RE: Python: file write issue

FallFromINFINITY
Member

Posts: 9
Location:
Joined: 23.03.09
Rank:
Apprentice
Posted on 16-12-09 02:34
That worked.

The reason for needing them split is because I need the process to be able to create a list from the data in the file, but the list it needs to be edited and then written back to the file by the program, based on an input by the user.

It needs to be read and written from/to the file so it can be continued another time, because each time the program is run, it needs an input. It's a bit of a bookkeeping/statistics program.

In other words, I'm using the program to edit a "database" instead of doing it manually, which would require a lot more work.

Thanks. It's done, well, at least it's working.



One last thing. Although it's not needed, how would I make an if/elif statement that compares the item to more than one possiblility.
I tried:
if input == "one" or "two" or "three":
<tab>...
elif input == "three" or "four" or "five":
<tab>...
else: ...


But it only ever executes the "if" statement. and yes, the input is a string.

Again, thanks to all for everything. I'm semi-new to python. I've only ever worked with it as a calculator. So lists, files, and if/else statements are pretty new to me.
I'm more into html, css, php, and MySQL.


--------------------------------------------------------------------
Who is "General Error" and why is he reading my harddisk?
FallFromINFINITY@hotmail.com FallFromINFINITY
Author

RE: Python: file write issue

ynori7
Future Emperor of Earth



Posts: 1481
Location: #valhalla
Joined: 08.10.07
Rank:
Diabolical
Posted on 16-12-09 02:45
FallFromINFINITY wrote:
if input == "one" or "two" or "three":

That will always evaluate to true. It's basically saying:
if (input == "one") or ("two") or ("three")
And clearly "two" and "three" are not false expresblockedsions. This will be the case in pretty much every programming language.
You need to say:
if input == "one" or input=="two" or input=="three"




ynori7 http://halls-of-valhalla.org
Author

RE: Python: file write issue

FallFromINFINITY
Member

Posts: 9
Location:
Joined: 23.03.09
Rank:
Apprentice
Posted on 16-12-09 03:08
Awesome. thanks. that did it.

One last time, Thanks to all of you. this is gonna make finishing this a lot easier.


--------------------------------------------------------------------
Who is "General Error" and why is he reading my harddisk?
FallFromINFINITY@hotmail.com FallFromINFINITY
Author

RE: Python: file write issue

stealth-
Member



Posts: 999
Location: Eh?
Joined: 10.04.09
Rank:
God
Posted on 16-12-09 03:11
You may want to also look at the self python module for databases. It writes data in very basic databases that are easy to read and write to.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealth-x.com
http://www.stealth-x.com
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.