问题描述
我希望能够创建一个程序,最后可以保存带有自己的扩展名的文本文件。后来用户应该可以双击该文件来运行程序并打开该文件。
我需要知道如何使python程序成为默认程序文件打开时,用户双击它,也如何获得该文件,当程序开始运行。
python 2.7
mac os x 10.6和窗口7
编辑:
举个例子,我正在制作一个绘画程序。用户想要保存他正在处理的文件。我的程序将它保存为untitled.paint,稍后用户双击untitled.paint,并期望我的程序打开该文件。
有没有办法给我告诉操作系统打开所有以.paint结尾的文件与我的绘图程序。
我不能将它保存为.jpg,因为这样做不会保存层或其他任何东西。
不知道OSX,但在Windows中,你可以这样做:
$ b $ ol <
使用扩展 .paint
,它是用假想的pyth打开的在脚本 pypaint.py
可能会说明一些事情:
run_paint.bat:
@echo off
echo'clicked file is'%1
python path_to_pypaint.py%1
py_paint.py:
import sys
print'opening',sys.argv [1]
I want to be able to create a program that can save text files with my own extension at the end. later the user should be able to double click on that file to run the program and open that file.
I need to know how to make the python program the default program a file opens whenever the user double click on it , and also how to get that file when the program starts running.
python 2.7mac os x 10.6 and windows 7
edit:say as an example, i was making a paint program. the user wants to save the file he was working on. my program will save it as untitled.paint, later the user double click on untitled.paint and expects my program to open up that file.
is there a way for me to tell the operating system to open all files ending with .paint with my paint program.
I can't save it as a .jpg because that won't save the layers or anything else.
Don't know about OSX but in Windows you can do it as follows:
- Create a batch file
- Select a file of the type you want to open "automatically", and use "Open with..." in the context menu to select the batch file as the default program to use.
- The batch file will get the "clicked" file passed argument 1, which you can then pass to your Python script as an argument - it then receives it as sys.argv[1].
An example using an extension ".paint
" which is opened using a hypothetical python script pypaint.py
may clarify things:
run_paint.bat:
@echo off
echo 'clicked file is' %1
python path_to_pypaint.py %1
py_paint.py:
import sys
print 'opening', sys.argv[1]
这篇关于双击一个文件来运行python脚本。如何将该文件作为输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!