#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    How can i write two sys.argv to a file ( a name and a number)


    How can I take sys.argv and read it directly into a file. I want to read a name and a number to the file 'file1'

    ./pythonscript.py name number


    Code:
    #!/usr/bin/python
    # How can I write to a file
    
    import sys
    
    filename = sys.argv[1]
    n = int( sys.argv[2] )
    
    FILE = open( file1, "w" )
    
    print 'n' >>FILE
    
    FILE.close()

    Thanks
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Since you're using unix with most likely a shell that's a super set of sh , the easy way is

    $ echo arg1 arg2 > file1

    Oh. That would also work in DOS.

    In python, if you must, why bother converting the argument to int ? (Type testing is a reason.)
    Code:
    #!/usr/bin/python
    import sys
    
    with open('file1', "w" ) as FILE:
        FILE.write('{} {}\n'.format(*sys.argv[1:]))
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Appending File


    Thanks for the response.

    I plan to build on this script in building block fashion. One understandable step at a time.

    Could you explain the last line. As a beginner it is hard for me to read and understand.

    Is there a way to make each new entry append the file 'file1'?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Assuming you mean that each new entry should be appended to file1, open the file in append mode.
    open('file1','a')

    And you want me to explain
    FILE.write('{} {}\n'.format(*sys.argv[1:]))
    ?

    Firstly, it's in a context indicated by the "with" statement that starts the block. The context handles closing the file. The context assigned a value to FILE if opening succeeded. You can write your own contexts in python3 if your class has a __context__ method. Actually, I've never done it so never mind.

    Having arbitrarily decided that you'd like both arguments to appear on the same line with a space between them, I next used string formatting. In its simplest form, the string formatting mini-language is the simplest way to format data into a human readable string.
    'a string'.format
    is the format method of the string.
    format() replaces the {} curly brace pairs with (I think) the str(value) of successive arguments.

    Next consider slicing. LIST[n:] is a list containing the objects of LIST from item n to the end of LIST. Hence, sys.argv[1:] is a list of the two arguments from the command line---assuming the command was correctly invoked. You didn't have any error testing beyond a crude fail with ValueError. It looked accidental; I didn't feel badly about removing it.

    Finally, I hope someone else can explain the asterisk.
    When passing arguments to a function
    *iterable
    means to "unlist the items", or "tuple assign" the arguments to the parameter list.
    **dictionary
    means to assign as by key word the dictionary as arguments.
    Of course str.format takes an arbitrary number of arguments,

    Forget my explanation. Study this link
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo