如何解决我的工作脚本无法从快捷方式启动
我正在尝试创建一个python脚本,该脚本从文件夹中随机选择一个文件并运行它。我首先尝试使用exe文件(特别是视频游戏)的列表快捷方式专门对其进行尝试,并且它完全按预期工作-我可以双击.py文件,并从代码中引用的目录中运行一个随机文件。 / p>
from os import walk,system
from random import randint
GameList=[]
for root,dirs,files in walk("C:\\Users\\awh.1998\\Desktop\\Gaming\\Games"):
for filename in files:
GameList.append(filename) #adds all games in 'Games' folder to GameList as strings
RandomGame=GameList[randint(0,(len(GameList)-1))]
RandomGamePath = f"C:\\Users\\awh.1998\\Desktop\\Gaming\\Games\\{RandomGame}"
system('cmd /k {}'.format(RandomGamePath))
print(RandomGamePath)
我试图将其推广以便更广泛地使用,主要区别在于在较新的脚本中,我还使用了os模块来自动查找路径。当我打开Python解释器并从解释器本身启动脚本时,代码可以完美地工作,但是当我尝试使用快捷方式(对其他脚本完美地工作)时,它只会在解释器中打开空白控制台,并且什么也不做。
from random import randint
def splitkeep(string,delimiter):
Split = string.split(delimiter)
Split = [substr + delimiter for substr in Split]
Split = Split[:-1]
output = "".join(Split)
return output
Path = getcwd() #defines current directory as string
Path = splitkeep(Path,"\\")
FileList=[]
for root,files in walk(Path):
for filename in files:
FileList.append(filename) #adds all files in folder to FileList as strings
FileList.remove('RandomFile.py')
RandomFile=FileList[randint(0,(len(FileList)-1))]
RandomFilePath=Path+RandomFile
system('cmd /k {}'.format(RandomFilePath))
print(Path)
print(FileList)
print(RandomFilePath)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。