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

Started by
16 comments, last by Chad Smith 9 years, 8 months ago

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!"
Advertisement

Look up the open function in python here

Now, what does it say about the 2nd parameter? You have "r+". What does that mean? Why is it the wrong thing?

STOP ASKING FOR HELP, AND USE THE TOOLS AVAILABLE TO YOU.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

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?
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

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

the problem from the windows ! bug in windows


It's not a bug in Windows, it's a bug in your code, admittedly an obscure one that Python programmers do not necessarily know to expect. This link explains what is going on, and you could've found it, I only had to type "python file read garbage" in google and it was the first hit wink.png

I would personally recommend staying away from the "+" mode suffixes because of these peculiarities and just seek wherever you need to write to, or, better yet (but not applicable to your case since you are learning how to work with files in Python) use something like pickle or JSON to automatically write your objects directly to disk and read them back painlessly in only a few lines of code.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

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

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 !

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

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()

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

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

This topic is closed to new replies.

Advertisement