双击一个文件来运行python脚本。如何将该文件作为输入? [英] double click a file to run python script. how to get that file as an input?

查看:1002
本文介绍了双击一个文件来运行python脚本。如何将该文件作为输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够创建一个程序,最后可以保存带有自己的扩展名的文本文件。后来用户应该可以双击该文件来运行程序并打开该文件。

我需要知道如何使python程序成为默认程序文件打开时,用户双击它,也如何获得该文件,当程序开始运行。

python 2.7
mac os x 10.6和窗口7



编辑:
举个例子,我正在制作一个绘画程序。用户想要保存他正在处理的文件。我的程序将它保存为untitled.paint,稍后用户双击untitled.paint,并期望我的程序打开该文件。



有没有办法给我告诉操作系统打开所有以.paint结尾的文件与我的绘图程序。

我不能将它保存为.jpg,因为这样做不会保存层或其他任何东西。

解决方案

不知道OSX,但在Windows中,你可以这样做:


$ b $ ol <
  • 创建一个批处理文件
  • 选择一个你想要自动打开的文件,然后使用打开方式...在上下文菜单中选择批处理文件作为默认程序使用。
  • 批处理文件将得到被点击的文件传递参数1,你可以然后传递给你的Python脚本作为参数 - 然后它接收为sys.argv [1]。

    使用扩展 .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.7 mac 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:

    1. Create a batch file
    2. 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.
    3. 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脚本。如何将该文件作为输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆