我正在尝试文件I/O操作。我有一个小练习程序,运行时会创建一个文本文件。我使用pyinstaller打包它,这样双击exe会创建一个新文件夹,并在其中放置一个带有“hello world”文本的文件。非常简单。
然后我开始思考main()
函数。这只是像其他任何函数一样的函数,对吧?那么这是否意味着我可以在运行时向其传递参数?
我在想Steam客户端,你可以在快捷方式中加入像“-dev”和“-console”之类的东西。有没有办法将这些东西添加到我创建的Python可执行文件中?
我的解释可能很糟糕,这里是一个例子:
def makeFile(string):
if string:
f = open('mytext.txt', 'w') #create text file in local dir
print >> f, 'hello, ' + string + '! \nHow are ya?'
f.close()
else:
f = open('mytext.txt', 'w') #create text file in local dir
print >> f, 'hello, person! \nHow are ya?'
f.close()
def main(string = None):
makeFile(string)
如果我将这段代码制作成exe文件,是否能够添加自己的可选参数?
我尝试了上面的代码,并用test.exe --"myname"
执行,但没有成功。
有什么方法可以实现这个功能吗?