我有一个python脚本读取的自定义文件类型(.photon),我通常在终端中运行:
py C:\Users\greym\Desktop\photon\photon.py C:\Users\greym\Desktop\photon\test.photon
最后一个参数是python脚本读取的文件,我是如何创建它的,所以我可以单击该文件,它将通过python脚本运行它
答案 0 :(得分:1)
==> ftype /? Displays or modifies file types used in file extension associations fileType Specifies the file type to examine or change openCommandString Specifies the open command to use when launching files of this type. … ==> assoc /? Displays or modifies file extension associations ASSOC [.ext[=[fileType]]] .ext Specifies the file extension to associate the file type with fileType Specifies the file type to associate with the file extension …
测试(请根据您的具体情况更改路径):
示例photon.py
脚本以二进制模式读取文件并将其内容打印到控制台:
import sys
if len( sys.argv) > 1:
file_name = sys.argv[1]
else:
file_name = 'D:\\test\\test.photon' # if no file name supplied
with open( file_name, 'rb') as f:
file_data = f.read()
print( file_data)
input( "Press Enter to continue...")
在提升的命令提示符下,只运行一次:
C:\Windows\system32> ftype photonfile=C:\Windows\py.exe -3 "D:\Python\photon.py" "%1" %*
photonfile=C:\Windows\py.exe -3 "D:\Python\photon.py" "%1" %*
C:\Windows\system32> assoc .photon=photonfile
.photon=photonfile
C:\Windows\system32>
然后,从任何命令提示符(或双击.photon
文件),运行everywhen:
==> "D:\test\test.photon"
b'\xc4\x9b\xc5\xa1\xc4\x8d\xc5\x99\xc5\xbe\xc3\xbd\xc3\xa1\xc3\xad\xc3\xa9'
Press Enter to continue...
==>
答案 1 :(得分:0)
对python2使用raw_input()
,对python3使用input()
。
将输出存储到变量并在需要时使用它。这将帮助您通过双击来使用它。