使用 python 命令执行该脚本文件_双击一个文件来运行python脚本。如何将该文件作为输入?...

1586010002-jmsa.png

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:

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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值