【问题标题】:How can I pass an argument while writing a file using python使用python编写文件时如何传递参数
【发布时间】:2016-05-05 19:31:08
【问题描述】:

我正在尝试使用 python 覆盖一个文件,我的代码看起来像这样:

from sys import argv
script = argv
Configuration_file = 'C:/Python33/argv.txt'
f= open(Configuration_file,'w')
f.write('script')

当我尝试使用命令使用命令提示符运行文件时

python argvnew.py 罗山,

其中 argvnew.py 是我的 python 文件,而 roshan 是我的论点。我希望 roshan 替换程序中提到的 argv.txt 文件中写入的任何内容。

这是正确的做法吗?

【问题讨论】:

  • 你试过了吗?
  • 是的,在从 sys import argv system = argv[1] Configuration_file = 'C:/Python33/argv.txt' f= open(Configuration_file,'w' ) f.write(系统)

标签: python-3.x command-line-arguments


【解决方案1】:

你可以通过调用sys.argv[]数组来获取参数。

sys.argv[0] 表示脚本名称本身。

然后你可以把它写回你的文件,打开文件比如here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 2012-03-19
    • 1970-01-01
    • 2021-12-30
    • 2016-12-22
    • 1970-01-01
    相关资源
    最近更新 更多