Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Python 2.7 - Save to file

Posted on 2016-09-21
4
Medium Priority
?
141 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
gelonida earned 2000 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 17

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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 …

610 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