Solved

Python 2.7 - Save to file

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
python pyusb how to find a file on mass storage 3 91
Python Assistance 7 86
Turning python script into an applet 12 116
Python 3.5.2 32 virtualenv problems 3 67
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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 …

856 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