Public Group

# Strange output when writing to a file object and printing it out

This topic is 1425 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

HI,

sorry for asking i really tried but i couldn't know what should i do

When I run the following code:

from sys import argv

def print_all(k):

usingfile.write("i want to make it write in the file then print the file")

print_all(usingfile)

print "Done!"


see the following result:

I tried changing the code to this:

from sys import argv

def print_all(k):
k.write("i want to make it write in the file then print the file")

print_all(usingfile)

print "Done!"

Edited by koka282

##### Share on other sites
i'm not 100% familiar with python, but assuming it's i/o system is like other file streams in other languages, then you need to rewind(or seek) back to the beginning of the file to read it's content. i'm surprised that python isn't returning an empty string, but maybe calling read while at the EOF is undefined?

##### Share on other sites

Modes 'r+''w+' and 'a+' open the file for updating (reading and writing); note that 'w+' truncates the file. Append 'b' to the mode to open the file in binary mode, on systems that differentiate between binary and text files; on systems that don’t have this distinction, adding the 'b' has no effect

! bug in windows !bug specific to Windows

usingfile=open(addingfile,"rb+")


still the same problem but i will try

Edited by koka282

##### Share on other sites

i Tried many times again

my last code was

from sys import argv

usingfile.write("i want to write in the file then print the file")

def rewind(k):
k.seek(0)

rewind(usingfile)

def print_all(k):

print_all(usingfile)

print"Done!"



i will try again to know it (y)

Bacterius

i will read but after trying again (y)

i hope i will write the answer

Edited by koka282

##### Share on other sites

i should type Seek between write & Read i tried before

but i will see what the problem where should i type seek !

script, addingfile = argv[0],argv[1]

def print_all(k):
k.seek(0)

usingfile.write("i want to make it write in the file then print the file")

print_all(usingfile)

print "Done!"


from sys import argv

def print_all(k):
k.write("i want to make it write in the file then print the file")
k.seek(0)

print_all(usingfile)

print "Done!"



i will try again !

Never Give Up !

Edited by koka282

##### Share on other sites

Yeaaaah !!!

from sys import argv

def print_all(k):
k.write("i want to make it write in the file then print the file")
k.seek(0)

print_all(usingfile)

print "Done!"



but i should type ("w+")

w+ : Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing

r+    : Opens a file for both reading and writing. The file pointer will be at the beginning of the file.

i can't type r+ !!!

(The file pointer will be at the beginning of the file)

i type seek(0) so r+ should run without problem !

Edited by koka282

##### Share on other sites

As was suggested, using the + options on files is sticky.  Instead, open a file for writing, write to it, close it, and then open it for reading:

file = open(fileName, "w")
file.write("Some string")
file.close()

file = open(fileName, "r")
file.close()


Or, a better way IMO:

# open the file stored in fileName, write some string to it, and it automatically closes
open(fileName, "w").write("Some String")

# open the file stored in fileName, read some data from it, print it and it automatically closes


##### Share on other sites

i will try that :) u think now i can go to the next tutorial ?

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 35
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631702
• Total Posts
3001812
×