Solved

Python 2.7 - Save to file

Posted on 2016-09-21
4
74 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

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…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

726 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