0

I have a function that reads a given file as an argument, I would like to make a script.

Here what I done :

import sys

def read_list_from_file(filename):
    with open(filename) as fobj :
        lines = fobj.read().splitlines()
    return lines

filenames = sys.argv[0]
if len(filenames) > 1 : read_list_from_file(filenames)

But nothing happens

1 Answer 1

1

Nothing happens as in what should happen? One thing that the sys.argv[0] is the script name and sys.argv[1] is the first argument you pass to the script.

Also you have not added any print statements. The below with print gives output.

import sys

def read_list_from_file(filename):
    with open(filename) as fobj :
        lines = fobj.read().splitlines()
    return lines

filenames = sys.argv[1]
print(filenames)
print(len(filenames))
if len(filenames) > 1 : 
    print(read_list_from_file(filenames))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.