【问题标题】:batch file to open custom filetype批处理文件以打开自定义文件类型
【发布时间】:2026-02-11 05:10:02
【问题描述】:

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

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

最后一个参数是python脚本读取的文件,我是如何制作它的,所以我可以点击该文件,它将通过python脚本运行它

【问题讨论】:

  • 您使用的是什么操作系统?窗户?
  • windows 10 pro 64 位
  • 为什么不使用 raw_input if python2 或 input for python3
  • 文件名需要使用两次,让用户输入两次效率低下
  • 用户将其输入一次,将其存储在一个变量中,然后使用它 100 次。没有区别。

标签: python file batch-file


【解决方案1】:

您可能对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...

==>

【讨论】:

    【解决方案2】:

    对 python2 使用 raw_input(),对 python3 使用 input()

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

    【讨论】:

      最近更新 更多