问题描述
我有一个.BAT文件。我用Python编写的编码文件。我想将此文件添加到任务计划程序中并每天自动运行,但是当我打开.BAT文件时,打开文件后立即出现命令窗口并突然关闭。我在Python中测试了此代码,并看到了它的作用。
然后,当我双击扩展名为.PY的文件并打开它时,命令窗口将打开并且代码可以正常工作。 .PY扩展名不适用于任务计划程序。我该如何解决,如果您能提供帮助,我将非常高兴。
import tweepy
import time
auth = tweepy.OAuthHandler('*','*')
auth.set_access_token('*','*')
api = tweepy.API(auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
user = api.me()
search = 'books'
nrTweets = 500
import time
time_limit_sec = 10 * 60
start_time = time.time()
for tweet in tweepy.Cursor(api.search,search).items(nrTweets):
if (time.time()-start_time) > time_limit_sec:
break
try:
print('Tweet Liked')
tweet.favorite()
time.sleep(10)
except tweepy.TweepError as e:
print(e.reason)
except stopiteration:
break
解决方法
基本上,当您双击.py文件时,它将在解释器中运行代码。如果您想在终端中运行脚本,则只需调用python并将脚本路径作为第二个参数传递。
以下解决方案将取决于您安装Python.exe的位置。在.bat文件中,添加以下行:
如果您的路径中有python.exe,则可以这样做
python "C:\Users\Desktop\path_to\python_file.py"
如果要明确说明安装python.exe的位置:
"C:\Users\Programs\path_to_python_installation\python.exe" "C:\Users\Desktop\path_to\python_file.py"