【问题标题】:Convert hard coded file names to command line arguments [duplicate]将硬编码文件名转换为命令行参数[重复]
【发布时间】:2015-03-30 04:40:27
【问题描述】:

run.py的脚本如下:

a = open('a.csv')
b = open('b.csv')
c = open('c.csv','w')

while True:
   la = a.readline()
   if not la: break
   lb = b.readline()
   la = la.split('\t')
   lb = lb.split('\t')
   la[4] = str(int(la[4])+int(lb[4]))
   la[5] = str(int(la[5])+int(lb[5]))
   c.write('\t'.join(la)); c.write('\n')

是否可以转换成如下格式:

python run.py a.csv b.csv c.csv

这样我就可以在终端中将文件名更改为参数,非常感谢。

【问题讨论】:

    标签: python csv


    【解决方案1】:

    您可以通过sys.argv 访问传递给您的程序的参数。

    from sys import argv
    
    a = open(argv[1])
    b = open(argv[2])
    c = open(argv[3],'w')
    
    # Etc.
    

    【讨论】:

      猜你喜欢
      • 2020-06-19
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      • 2020-06-27
      相关资源
      最近更新 更多