如何制作可以用文件打开的程序? (python)
How to make a program that can be opened with a file? (python)
我正在制作一个程序来打开一个文件并对它做一些事情,我想知道是否有一种方法可以让你点击一个文件,它会在程序中打开它,而不是去进入程序,单击“打开”,然后在文件中导航以找到它,或者只是一种可以单击 "Open With..." 和 select 您的程序的方法。这是代码,如果它有帮助的话:
from tkinter import *
from tkinter import filedialog
from subprocess import *
import os
root = Tk()
root.title("Snake converter")
def open_file():
filename = filedialog.askopenfilename(filetypes = (("Snake files", "*.sim"),("Python Files", "*.py"),("All files", "*.*")))
filenametmp = filename + ".tmp"
print filename + " is being compiled. Please wait..."
tf = open(filenametmp, "a")
f = open(filename, "r")
filecontents = f.read()
tf.write("from simincmodule import *" + "\n")
tf.write(filecontents)
os.chdir("C:\Snake\info\Scripts")
print os.getcwd()
call(["pyinstaller", filenametmp])
os.remove("C:/Snake/info/Scripts/build")
f.close()
tf.close()
print "\n\n----------------------------------------------------------------------\n\nFinished compiling " + filename + ". Find the program under [filename]/[filename].exe"
openbutton = Button(root, text = "Open", width = 10, command = open_file)
openbutton.pack()
root.mainloop()
如有任何帮助或建议,我们将不胜感激。
谢谢!
我会怎么做:
- 让您的程序从标准输入读取并写入标准输出
- 使用shell的力量。如果你在 unix shell 中,只需简单地做
cat infile | ./python myProgram.py > outfile
这会将 infile
的内容提供给 stdin 上的程序,然后将 stdout 的输出写入 outfile
。
"Open with..." 通常将文件的路径名发送到 sys.argv
。所以在你的程序的适当位置,添加这个:
if len(sys.argv) > 1:
open_file(sys.argv[1])
(正如我在评论中所说,你真的想让你的 open_file
接受一个参数,并有另一个像 open_file_dialog
这样的函数来打开对话框。)
首先就留下了如何制作可以 "Open with..." 的问题。如果您使用 Windows,您应该能够通过编辑注册表来更好地控制文件关联:有关详细信息,请参阅 this MSDN page。
或者,一种快速而肮脏的方法是制作一个 .bat
脚本,该脚本接受一个参数并将其传递给您的 python 程序。我记得前段时间做过这个,但是我已经很久没有认真使用过Windows,所以我不能直接告诉你如何写脚本。
我是 Autohotkey 工具和 Python 语言的粉丝,
另一种方法是:
如果你想运行一个程序,然后想用一个文件打开它("Open With...")
你可以想想,
使用键盘快捷键宏制作您自己的电脑动作脚本。
步骤 1: 在您的 Windows 系统上安装 (Python27)。 Click Here
步骤 2: 然后安装 Python 软件包 - pyautogui 和 pywinauto
您可以使用这个 Msdos 批处理脚本:
Install.bat
C:\Python27\scripts\pip.exe install pyautogui
pause
C:\Python27\scripts\pip.exe install pywinauto
pause
现在您已准备好制作和使用此 Python 脚本:
Example1.pyw
#run Notepad with "Open..."
#######################
import pywinauto
pywinauto.Application().start(r"C:\Windows\System32\Notepad.exe c:\test\test.txt")
#######################
Example2.pyw
#run Notepad
#######################
import pywinauto
pywinauto.Application().start("C:\Windows\System32\Notepad.exe")
#######################
#Open a File - "Open With..."
#######################
import pyautogui
import time
time.sleep(2)
pyautogui.hotkey('ctrl','o') #Many Programs use Shortcut Ctrl+o to "Open With..."
time.sleep(.750)
pyautogui.typewrite('c:\test\test.txt',0)
time.sleep(2)
pyautogui.hotkey('enter')
#######################
# you can send any text or Keyboard Shortcuts Combinations - Example Copy - pyautogui.hotkey('ctrl', 'c')
注意:如果您将文件路径与 typewrite 命令一起使用 - 您不能放置(单反斜杠 \),您必须将其替换为(双反斜杠 \\)
提示:Python 语言与自动 Python 启动器软件是一个很好的组合 - 如果你想制作,Windows 桌面上的工具栏 - [=53 的可执行图片=] 鼠标或触摸设备的脚本。 - 有关更多信息,请查看 Homepage
我正在制作一个程序来打开一个文件并对它做一些事情,我想知道是否有一种方法可以让你点击一个文件,它会在程序中打开它,而不是去进入程序,单击“打开”,然后在文件中导航以找到它,或者只是一种可以单击 "Open With..." 和 select 您的程序的方法。这是代码,如果它有帮助的话:
from tkinter import *
from tkinter import filedialog
from subprocess import *
import os
root = Tk()
root.title("Snake converter")
def open_file():
filename = filedialog.askopenfilename(filetypes = (("Snake files", "*.sim"),("Python Files", "*.py"),("All files", "*.*")))
filenametmp = filename + ".tmp"
print filename + " is being compiled. Please wait..."
tf = open(filenametmp, "a")
f = open(filename, "r")
filecontents = f.read()
tf.write("from simincmodule import *" + "\n")
tf.write(filecontents)
os.chdir("C:\Snake\info\Scripts")
print os.getcwd()
call(["pyinstaller", filenametmp])
os.remove("C:/Snake/info/Scripts/build")
f.close()
tf.close()
print "\n\n----------------------------------------------------------------------\n\nFinished compiling " + filename + ". Find the program under [filename]/[filename].exe"
openbutton = Button(root, text = "Open", width = 10, command = open_file)
openbutton.pack()
root.mainloop()
如有任何帮助或建议,我们将不胜感激。
谢谢!
我会怎么做:
- 让您的程序从标准输入读取并写入标准输出
- 使用shell的力量。如果你在 unix shell 中,只需简单地做
cat infile | ./python myProgram.py > outfile
这会将 infile
的内容提供给 stdin 上的程序,然后将 stdout 的输出写入 outfile
。
"Open with..." 通常将文件的路径名发送到 sys.argv
。所以在你的程序的适当位置,添加这个:
if len(sys.argv) > 1:
open_file(sys.argv[1])
(正如我在评论中所说,你真的想让你的 open_file
接受一个参数,并有另一个像 open_file_dialog
这样的函数来打开对话框。)
首先就留下了如何制作可以 "Open with..." 的问题。如果您使用 Windows,您应该能够通过编辑注册表来更好地控制文件关联:有关详细信息,请参阅 this MSDN page。
或者,一种快速而肮脏的方法是制作一个 .bat
脚本,该脚本接受一个参数并将其传递给您的 python 程序。我记得前段时间做过这个,但是我已经很久没有认真使用过Windows,所以我不能直接告诉你如何写脚本。
我是 Autohotkey 工具和 Python 语言的粉丝,
另一种方法是:
如果你想运行一个程序,然后想用一个文件打开它("Open With...")
你可以想想,
使用键盘快捷键宏制作您自己的电脑动作脚本。
步骤 1: 在您的 Windows 系统上安装 (Python27)。 Click Here
步骤 2: 然后安装 Python 软件包 - pyautogui 和 pywinauto
您可以使用这个 Msdos 批处理脚本:
Install.bat
C:\Python27\scripts\pip.exe install pyautogui
pause
C:\Python27\scripts\pip.exe install pywinauto
pause
现在您已准备好制作和使用此 Python 脚本:
Example1.pyw
#run Notepad with "Open..."
#######################
import pywinauto
pywinauto.Application().start(r"C:\Windows\System32\Notepad.exe c:\test\test.txt")
#######################
Example2.pyw
#run Notepad
#######################
import pywinauto
pywinauto.Application().start("C:\Windows\System32\Notepad.exe")
#######################
#Open a File - "Open With..."
#######################
import pyautogui
import time
time.sleep(2)
pyautogui.hotkey('ctrl','o') #Many Programs use Shortcut Ctrl+o to "Open With..."
time.sleep(.750)
pyautogui.typewrite('c:\test\test.txt',0)
time.sleep(2)
pyautogui.hotkey('enter')
#######################
# you can send any text or Keyboard Shortcuts Combinations - Example Copy - pyautogui.hotkey('ctrl', 'c')
注意:如果您将文件路径与 typewrite 命令一起使用 - 您不能放置(单反斜杠 \),您必须将其替换为(双反斜杠 \\)
提示:Python 语言与自动 Python 启动器软件是一个很好的组合 - 如果你想制作,Windows 桌面上的工具栏 - [=53 的可执行图片=] 鼠标或触摸设备的脚本。 - 有关更多信息,请查看 Homepage