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

## 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 ?

##### Share on other sites

iam ready for next tutorial  i tried everything

Edited by koka282

##### 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 on other sites

yes i looked at all

but all gave me down vote why

Edited by koka282

##### 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]

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 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.

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

##### Share on other sites

...............

Lysy :)

i try bro :)

Sorry all :)

Edited by koka282

##### 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?

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627700
• Total Posts
2978690

• 20
• 14
• 12
• 10
• 12