Python 批处理文件以打开自定义文件类型

Python 批处理文件以打开自定义文件类型,python,file,batch-file,Python,File,Batch File,我有一个自定义文件类型(.photon),由python脚本读取,我通常在终端中这样运行: py C:\Users\greym\Desktop\photon\photon.py C:\Users\greym\Desktop\photon\test.photon 最后一个参数是python脚本读取的文件,我是如何创建它的,因此我可以单击该文件,它将通过python脚本运行它,python2使用raw\u input(),python3使用input() 将输出存储到变量,并在需要时使用它。这将通

我有一个自定义文件类型(.photon),由python脚本读取,我通常在终端中这样运行:

py C:\Users\greym\Desktop\photon\photon.py C:\Users\greym\Desktop\photon\test.photon
最后一个参数是python脚本读取的文件,我是如何创建它的,因此我可以单击该文件,它将通过python脚本运行它,python2使用
raw\u input()
,python3使用
input()


将输出存储到变量,并在需要时使用它。这将通过双击帮助您使用它。

您可能会对以下命令感兴趣:

测试(请根据您的情况更改路径):

示例
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
文件),运行以下各项:

==> "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...

==>

您使用的是什么操作系统?Windows?Windows 10 pro 64位为什么不使用原始输入?如果python2或Python3的输入文件名需要使用两次并让用户输入两次,则效率低下用户将输入一次并将其存储在变量中,然后使用100次。没什么区别。
==> "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...

==>