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

2024-06-16 10:39:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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

py C:\Users\greym\Desktop\photon\photon.py C:\Users\greym\Desktop\photon\test.photon

最后一个参数是python脚本读取的文件,我是如何创建它的,这样我就可以单击该文件,它将通过python脚本运行它


Tags: 文件pytest脚本终端参数users文件类型
2条回答

您可能对FTYPEASSOC命令感兴趣:

==> 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文件)运行以下所有操作:

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

==>

使用raw_input()表示python2,input()表示python3。你知道吗

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

相关问题 更多 >