Reading and Writing Files

A forum for general discussion of the Python programming language.

Reading and Writing Files

Postby HTHVampire » Sun Apr 14, 2013 1:41 pm

Hi guys, I'm a beginner in learning Python and I'm currently working on "Learn Python The Hard Way".
Here's the script of my question:
http://codepad.org/JcQ8916J

And this is the result of the script:
Image

in line 12, what does the code 'w' means? will there be any difference if I open the file without 'w'?

Your guides are much appreciated! Thanks for your help!
HTHVampire
 
Posts: 9
Joined: Sun Apr 14, 2013 1:31 pm

Re: Reading and Writing Files

Postby Yoriz » Sun Apr 14, 2013 3:30 pm

The link you posted is not working for me.
Please read this post with regards on how to post questions.
Im guessing that line 12 opens a file, the 'w' set the mode of opening the file to write see this for the other modes.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 840
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Reading and Writing Files

Postby metulburr » Sun Apr 14, 2013 5:48 pm

Code: Select all
from sys import argv

script, filename = argv

print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."

raw_input("?")

print "Opening the file..."
target = open(filename, 'w')

print "Truncating the file. Goodbye!"
target.truncate()

print "Now I'm going to ask you for three lines."

line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")

print "I'm going to write these to the file."

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print "And finally, we close it."
target.close()

please just post the code here. That is why we have code tags. Plus you could of just copy/pasted the output instead of posting a screenshot.

opening a file in 'w' mode is 'write' mode, get it? (w=write). If you left out the 'w', it would go to default which is 'r' mode, read mode. So hense you could not write to it. Some others are:

a = append
rb = read byte
wb = write byte
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Reading and Writing Files

Postby stranac » Sun Apr 14, 2013 6:17 pm

metulburr wrote:rb = read byte
wb = write byte

It's actually binary...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: Reading and Writing Files

Postby HTHVampire » Mon Apr 15, 2013 12:11 pm

Yoriz wrote:The link you posted is not working for me.
Please read this post with regards on how to post questions.


metulburr wrote:
please just post the code here. That is why we have code tags. Plus you could of just copy/pasted the output instead of posting a screenshot.


Hey, Thanks you guys for guiding me the way to post my question properly. and thanks for your answers, that really help me to understand my script.

Ok, here's another question of mine regarding the script.
Code: Select all
from sys import argv

script, filename = argv

print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."

raw_input("?")

print "Opening the file..."
target = open(filename, 'w')

print "Truncating the file. Goodbye!"
target.truncate()

print "Now I'm going to ask you for three lines."

line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")

print "I'm going to write these to the file."

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print "And finally, we close it."
target.close()


May I know that how can I use strings, formats, and escapes to print out line1, line2, line3 with just one target.write() command instead of 6? since there are too much of repetition for the command target.write().

Thanks for your time!
HTHVampire
 
Posts: 9
Joined: Sun Apr 14, 2013 1:31 pm

Re: Reading and Writing Files

Postby metulburr » Mon Apr 15, 2013 12:27 pm

either concatenate line 1, 2, and 3 with newlines bnetween them and just write that one string to the file, or format into the string to be written

Code: Select all
target.write('{}\n{}\n{}\n'.format(line1,line2,line3))
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Reading and Writing Files

Postby HTHVampire » Thu Apr 25, 2013 12:52 pm

metulburr wrote:either concatenate line 1, 2, and 3 with newlines bnetween them and just write that one string to the file, or format into the string to be written

Code: Select all
target.write('{}\n{}\n{}\n'.format(line1,line2,line3))


Hi, sorry for the late reply, I was on vacation. Thanks for your solution and time for my problem. I really appreciate that!
HTHVampire
 
Posts: 9
Joined: Sun Apr 14, 2013 1:31 pm


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests