[英]Executing a file using a Python script
I don't have a clue on where to look for a solution to this.我不知道在哪里寻找解决方案。 Basically i want to make a file that can be "opened with" a python script.
基本上我想制作一个可以用 python 脚本“打开”的文件。 The directory of the opened file will be stored as a variable in the script.
打开文件的目录将作为变量存储在脚本中。 Here's what I mean by "Open With":
这就是我所说的“打开方式”:
Sorry if I'm not too good at describing this.对不起,如果我不太擅长描述这一点。
There might be a more direct way, but one approach could be to write a simple batch file that invokes the Python interpreter with your script:可能有更直接的方法,但一种方法可能是编写一个简单的批处理文件,使用您的脚本调用 Python 解释器:
main.bat
@echo off
python main.py %1
Add absolute or relative paths if necessary.如有必要,添加绝对或相对路径。
Within main.py
, the name of the file should then be available as sys.argv[1]
.在
main.py
中,文件名应该可以作为sys.argv[1]
使用。
If I'm not misunderstanding, what you want is your argv[1] be 'C:.PythonCode2\runfileswith\test.phktest'.如果我没有误解,你想要的是你的 argv[1] 是'C:.PythonCode2\runfileswith\test.phktest'。
I did some test on my Windows 10, simply drag the .phktest
file onto the main.py
will let sys.argv[1] be the full path of that file!我在我的 Windows 10 上做了一些测试,只需将
.phktest
文件拖到main.py
上即可让 sys.argv[1] 成为该文件的完整路径!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.