从cmd运行且未按计划执行时,脚本找不到文件

时间:2018-12-25 16:31:38

标签: python scheduled-tasks praw os.path

制作reddit机器人(基于https://www.pythonforengineers.com/build-a-reddit-bot-part-1/)。代码通常有效,遵循问题。脚本和.txt文件位于我的LiClipse Workspace文件夹中(C:\ Users [me] \ Documents \ LiClipse Workspace \ Wall_of_Text_Bot)Python位于单独的位置,C:\ Users [me] \ AppData \ Local \ Programs \ Python \ Python37

praw.ini文件的副本位于C:\ Users [me] \ AppData \ Roaming中。 (这与以前一样需要,当从命令行praw运行时找不到我的Bot1信息。)

问题:

  1. 我对其进行了修改,以制作一个运行时文件来跟踪其运行时间。由于某种原因,它不会创建文件(无论是在LiClipse还是其他位置运行)。当我手动创建bot_runtimes.txt时,它将识别并写入该文件。 (这些是开始时紧随导入之后的if和else块。)是否有未创建文件的原因?

  2. 但是,如果我从cmd运行,它将无法看到或创建文件。这是由于os.path在哪里寻找东西?还是我忽略的其他事情?从cmd运行时,该机器人可以找到“ posts_replied_to文件,但从cmd运行时,它会打印出“没有bot运行时文件”(尽管答复和运行时文件都在同一目录中)。

    < / li>
  3. 我似乎无法让调度程序执行脚本。 (或者也许由于1和2中的问题而没有保留任何日志?)我使用任务计划程序,将程序设置为C:\ Users [me] \ AppData \ Local \ Programs \ Python \ Python37 \ python.exe和参数为“ C:\ Users [me] \ Documents \ LiClipse Workspace \ Wall_of_Text_Bot \ Wall_of_Text_Bot.py”,我没有收到任何错误消息,甚至在强制运行时也看不到命令​​行或IDLE弹出。

感谢您的帮助。谢谢。

import praw
# import pdb was in guide file but not used
# import re was in  guide script also not used 
import os
import datetime

import nltk
#nltk.download("punkt")

    #******here are the blocks re: os.path questions*******

if not os.path.isfile('posts_replied_to.txt'):
    posts_replied_to = []
    print("Did't have initial replied to file")
    #working

else:
    with open('posts_replied_to.txt') as f:
        # needed to add variable f for subsequent lines
        posts_replied_to = f.read()
        posts_replied_to = posts_replied_to.split('\n')
        posts_replied_to = list(filter(None, posts_replied_to))
    print("Opened the reply file.")
    #working

if not os.path.isfile('bot_runtimes.txt'):
    bot_runtimes = []
    print("Did't have bot runtimes file")
    #working

else:
    with open('bot_runtimes.txt') as f:
        # needed to add variable f for subsequent lines
        bot_runtimes = f.read()
        bot_runtimes = bot_runtimes.split('\n')
        bot_runtimes= list(filter(None, bot_runtimes))
        print("Opened the runtime file.")

    with open('bot_runtimes.txt', 'a') as f:   
        print(now)
        f.write(now + '\n')
        print("Wrote new run time to the file")

# ****** here is how the bot reads and writes, this works when ran through LiClipse 

subreddit = r.subreddit('pythonforengineers')
for submission in subreddit.hot(limit=5):
    if submission.id not in posts_replied_to:
        print ("Sub ID not in replied file.")

        data = submission.selftext
        words = data.split(" ")
        numwords = len(words)
        lines = data.split("\n")
        numlines = len(lines)

        if numwords > 100 and numlines == 1:
            submission.reply("""Hello, I couldn't help but notice that your submission is a very long paragraph.  Unfortunately, text without paragraph breaks, like yours, is very hard to read.
To help more people read and enjoy your text, please use more paragraph (line) breaks.  If you're not sure where to put them, here are some good resources:

https://www2.open.ac.uk/students/skillsforstudy/dividing-your-work-into-paragraphs.php
http://www.saidsimple.com/content/100835/
http://apps.prsa.org/intelligence/tactics/articles/view/10215/1078/cut_it_down_readers_skip_long_paragraphs#.xbwhavlkjiu

Beep boop.  I'm a bot.  If you think this was in error, you can message me.  I'll probably ignore it.  Beep boop.
            """)
            print('Bot replying type 1 to: ', submission.title)
            posts_replied_to.append(submission.id)
            with open('posts_replied_to.txt', 'w') as f:
                for post_id in posts_replied_to:
                    f.write(post_id + 'type 1' + '\n')

        else:
            for l in lines:
                wordsperline = len(l.split(" "))
                sentencesnltk = len(nltk.sent_tokenize(l))

                if wordsperline > 500 or sentencesnltk > 10:
                    submission.reply("""Hello, I couldn't help but notice that at least one of your paragraphs is very long.  Unfortunately, text without paragraph breaks, like yours, is very hard to read.
To help more people read and enjoy your text, please use more paragraph (line) breaks.  If you're not sure where to put them, here are some good resources:

https://www2.open.ac.uk/students/skillsforstudy/dividing-your-work-into-paragraphs.php
http://www.saidsimple.com/content/100835/
http://apps.prsa.org/intelligence/tactics/articles/view/10215/1078/cut_it_down_readers_skip_long_paragraphs#.xbwhavlkjiu

Beep boop.  I'm a bot.  If you think this was in error, you can message me.  I'll probably ignore it.  Beep boop.
            """)

                    print('Bot replying type 2 to: ', submission.title)
                    posts_replied_to.append(submission.id)
                    with open('posts_replied_to.txt', 'w') as f:
                        for post_id in posts_replied_to:
                            f.write(post_id + 'type 2' + '\n')

0 个答案:

没有答案