python带参数执行py文件
有勇气的牛排
268
Python
2023-05-18 21:04:05
"""
python 执行带参数
python test.py test.txt /home/charles /home/charles/target
"""
if __name__ == '__main__':
import sys
args = sys.argv
print(args)
py_file = args[0]
data_file_name = args[1]
data_file_path = args[2]
target_path = args[3]
print(py_file)
print(data_file_name)
print(data_file_path)
print(target_path)
<pre><div class="hljs"><code class="lang-python"><span class="hljs-string">"""
python 执行带参数
python test.py test.txt /home/charles /home/charles/target
"""</span>
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
<span class="hljs-keyword">import</span> sys
args = sys.argv
<span class="hljs-built_in">print</span>(args)
<span class="hljs-comment"># ['test.py', 'my.txt', '/home/charles', '/home/charles/target']</span>
py_file = args[<span class="hljs-number">0</span>] <span class="hljs-comment"># 主文件 主文件路径省略,在当前文件夹运行</span>
data_file_name = args[<span class="hljs-number">1</span>] <span class="hljs-comment"># 数据文件</span>
data_file_path = args[<span class="hljs-number">2</span>] <span class="hljs-comment"># 数据文件 路径</span>
target_path = args[<span class="hljs-number">3</span>] <span class="hljs-comment"># 结果存放路径</span>
<span class="hljs-built_in">print</span>(py_file)
<span class="hljs-built_in">print</span>(data_file_name)
<span class="hljs-built_in">print</span>(data_file_path)
<span class="hljs-built_in">print</span>(target_path)
</code></div></pre>
留言