Sign in to follow this  

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

This topic is 1210 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
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

We can't know if you're ready for the next tutorial or not.

If you feel like you understand all/most of the stuff that's been gone through so far in the tutorials, I would suggest moving forward.

If you feel there are things you don't understand completely, or things that you're uncertain about, I would suggest taking a closer look at those things, before you move on to the next tutorial.

 

In the end, that's a choice you'll have to make on your own.

Share this post


Link to post
Share on other sites

Yeaaaah !!!

from sys import argvscript, 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+ : [background=#f7f7f7]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[/background]
[background=#f7f7f7]r+ : [/background][background=#f7f7f7]Opens a file for both reading and writing. The file pointer will be at the beginning of the file.[/background]
i can't type r+ !!!
([background=#f7f7f7]The file pointer will be at the beginning of the file)[/background]
[background=#f7f7f7]i[/background][background=#f7f7f7] type seek(0) so r+ should run without problem ![/background]

[background=#f7f7f7]The file pointer i will read about it [/background]

Does the file already exists?, it's likely r+ won't work if it can't find the requested file. However w+ will create the file if it does not exist. In the end this is a good exercise on error handling and validation techniques. Don't assume that the file has been opened correctly, always check that you have a valid file, and if you do not, then display an appropiate dialog to the user.

Edit: the reason you are likely being downvoted is because A: It is not our responsibilty to tell you when you can move on. And B: You don't appear to have a good grasp on how file handles work. Edited by slicer4ever

Share this post


Link to post
Share on other sites

Programming is just another name for 'problem solving'.

You should learn to solve problems by yourself, only this way you will become a programmer.

 

You don't need to hurry, it seems that you see something strange in your code/output, and you're posting a question here seconds later. This is just WRONG.

When you've got problem, consider at least googling it first. Experiment, try to solve it somehow. Have you solved any programming problem by yourself? Have you even tried?

Because when you solve a problem by yourself, that's where the satisfaction hits. Right now you're just copy/pasting code, not programming. Many users gave you many good advices here so far, yet you seem to ignore them all, and just waiting for your programming problems to be solved by someone else. You won't get any further if you keep doing it that way.

 

My advice:

Try not posting you problems here for a few days. and solve them on your own.

Share this post


Link to post
Share on other sites

I think it is noteworthy to see how many people is just assuming that they are talking to an adult here, given all the harsh answers.  While I agree that you can expect certain things from the older population here, killing the enthusiasm of the younger people who are just starting out as programmers is just wrong.  Regardless what is the case, I think there should be a bit more patience with people in the BEGINNERS forum.  Why are you even reading the threads here if you don't want to help?  Why are you even complaining, if you decide to help anyway?

Share this post


Link to post
Share on other sites

This topic is 1210 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this