Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to input multiple files in python script using sys.argv list

I want to take multiple files one by one into python script as input files and perform my script operations on all of those files one after another.I want to do this using sys.argv list but don't know how to do this properly. here is the piece of code I have:

import re
r=open('sys.argv[1]',"r")

What am I supposed to do first to initialize this list and then open them one by one to read by my script? Any help will be obliged. thanks

like image 334
iqra khan Avatar asked Sep 21 '26 02:09

iqra khan


1 Answers

If you need to use them one by one from command, you may use sys.argv[1:]

import sys
    
def main():
    for filename in sys.argv[1:]:
        with open(filename, 'r') as file:
            file.readlines()
    
if __name__ == '__main__':
    main()
like image 137
Taras Struk Avatar answered Sep 22 '26 15:09

Taras Struk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!