Jump to content
  • Advertisement
Sign in to follow this  
koka282

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

HI,

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

 

When I run the following code:

from sys import argv
script, addingfile = argv[0],argv[1]

def print_all(k):
    print k.read()

usingfile = open(addingfile, "r+")

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

print_all(usingfile)

print "Done!"

 see the following result:

 

 

image.png

 

I tried changing the code to this:

from sys import argv
script, addingfile = argv[0], argv[1]

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

usingfile = open(addingfile, "r+")

print_all(usingfile)

print "Done!"
Edited by koka282

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 smile.png

Edited by koka282

Share this post


Link to post
Share on other sites

i Tried many times again

my last code was smile.png

from sys import argv
script,addingfile=argv[0],argv[1]

usingfile=open(addingfile,"rb+")

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 k.read()




print_all(usingfile)

print"Done!"


i will try again to know it (y) smile.png

 

Bacterius

 

i will read but after trying again (y)

i hope i will write the answer 

Edited by koka282

Share this post


Link to post
Share on other sites

i should type Seek between write & Read i tried before smile.png

 

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)
     print k.read()

usingfile = open(addingfile, "r+")

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



print_all(usingfile)

print "Done!"

from sys import argv
script, addingfile = argv[0],argv[1]

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

usingfile = open(addingfile, "r+")



print_all(usingfile)

print "Done!"

i will try again ! 

Never Give Up !

Edited by koka282

Share this post


Link to post
Share on other sites

Yeaaaah !!!

from sys import argv
script, addingfile = argv[0],argv[1]

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

usingfile = open(addingfile, "w+")



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 !

 

The file pointer i will read about it 

Edited by koka282

Share this post


Link to post
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")
print file.read()
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
print open(fileName, "r").read()

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!