Solved

Python 2.7 - Save to file

Posted on 2016-09-21
4
58 Views
Last Modified: 2016-09-22
Hi there,

I want to output/append "print(int(argument2) + 100)" to the file name "text.txt"

import sys
args = sys.argv[1:]
if len(args) < 2:
    print("ERROR: at least 2 arguments required, but got only %d"  % len(args))
    sys.exit(1)

argument1, argument2  = args[:2]
argument2 = int(argument2)

print("argument1 %r" % argument1)
print("") #CLRF is intended here

print(int(argument2) + 100)

Open in new window


Thanks for your help,
Rene
0
Comment
Question by:ReneGe
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
gelonida earned 500 total points
ID: 41809576
below code shows you opening files with the with statement and without.

it shows you how to write to files with write or with print

and it shows you how to write print statements  in a way, that they work with python2 and with python3

from __future__ import print_function
import sys
args = sys.argv[1:]
if len(args) < 2:
    print("ERROR: at least 2 arguments required, but got only %d"  % len(args))
    sys.exit(1)

argument1, argument2  = args[:2]
argument2 = int(argument2)

print("argument1 %r" % argument1)
print("") #CLRF is intended here


# now let's write to file1.txt using open and the with statement
# the with statement will automatically close the file at the end of 
# the with block
with open("file1.txt", "a") as fout: # use upen("file1.txt", "w") to NOT append
    fout.write("%d\n" % (int(argument2) + 100))

# now let's write to file1.txt using open and no with statement.
# It's recommended to close the file explicitely, though in many cases
# your code will work even if you don't close the file.
fout  =  open("file2.txt", "a") 
fout.write("%d\n" % (int(argument2) + 100))
fout.close()

# now let's use the print statement
with open("file3.txt", "a") as fout:
    print(int(argument2) + 100, file=fout)

Open in new window

0
 
LVL 10

Author Comment

by:ReneGe
ID: 41809605
Thanks :)

Can you please explain:
-the %r in: print("argument1 %r" % argument1)
-"%d\n" and % in: fout.write("%d\n" % (int(argument2) + 100))

Thanks
0
 
LVL 16

Expert Comment

by:gelonida
ID: 41809776
Python has multiple ways of formatting variables to strings.

The 'old' way is using the % operator.
It is very similiar to the printf formatting of the C language.

the more 'modern' way is the format() method of string objects.

WIth the % operator you habe
 formatstring % (var1, var2, var3)

Open in new window


example:
a=1
b=2
print("A=%d B=%d and A+B=%d" % (a, b, a+b)) # the old % string format operatior
print("A={} B={} A+B={}".format(a, b, a+b)  # the newer more flexible, more complex format method

Open in new window


%d is for decimal numbers
%f for floating point numbers
%s for strings
%r for a representation of a variable

Open in new window

a=11
b="11"
print("a=%s and b=%s" % (a, b))  # results in a=11 and b=11
print("a=%r and b=%r" % (a, b))  # results in a=11 and b='11'

Open in new window


so if you use %r you can see, that a is a number and b is a string

more details about the % operator at https://docs.python.org/2/library/stdtypes.html#string-formatting

more details about the format() method at https://docs.python.org/2/library/string.html#format-string-syntax
0
 
LVL 10

Author Comment

by:ReneGe
ID: 41810217
Thank you sooo much for your explanation :)

Cheers!!!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
converting return from type() function in python to string 1 78
Loaded data from a remote database into excel spreadsheet 2 116
Python tuples 2 120
Python output problem 10 54
The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now