Search code examples
pythonargumentswith-statementsys

How to pass Sys.argv[n] to a function from inside if "__name__" == "__main__"


Hi guys I'm trying to write a code that scrapes websites and get the links from the websites. Now I'm trying to implement that a user will give the script a link_list.txt that the script will iterate over.

However, I'm not successful with my Sys.argv[]

Here is some of the code:

    def link_list_maker(self, link_list_path):
    self.link_list_path = link_list_path
    with open(link_list_path, 'r') as list:
        line = list.readline()
        print(line)  # expecting a line read


if __name__ == "__main__":
    CrawlerClass.link_list_maker(sys.argv[1])
    crawler = CrawlerClass("http://quotes.toscrape.com/")  # TODO: Make a sysargv to work wth link list from user
    crawler.start()

As you can see, I'm trying to give the CrawlerClass.link_list_maker() function a sys.argv, but all I get is: TypeError: link_list_maker() missing 1 required positional argument: 'link_list_path'

i run the script with: python3 <script_name> <sys.argv[1]>

How do I pass the argv[1] correctly to the function?


Solution

  • Did you mean:

    if __name__ == "__main__":
        crawler = CrawlerClass("http://quotes.toscrape.com/")  # TODO: Make a sysargv to work wth link list from user
        crawler.link_list_maker(sys.argv[1])
        crawler.start()