user2991305
user2991305

Reputation: 11

How to schedule a Python script to run at a certain time?

Hello I am very new to programming and i'm trying to make an auto-posting bot for my subreddit.I'm using praw and I need to run this script at certain times and have it input and work

import praw

r = praw.Reddit(user_agent="UA")
r.login("username", "password")
sub = r.get_subreddit("Sub")
sub.submit("Title", text="Post text")

I'm running windows and someone said to use the task scheduler but I haven't been able to figure it out. Any help would be great. Thank You.

Upvotes: 1

Views: 8820

Answers (1)

go2
go2

Reputation: 378

I would suggest to look into sched, a general purpose event scheduler. It is described, with appropriate examples to start you, in the Python's documentation.

Sample:

import time
import sched

scheduler = sched.scheduler(time.time, time.sleep)

def reddit():
  <your code>

def scheduler_reddit():

  scheduler.enter(0, 1, reddit, ())
  scheduler.run()
  time.sleep(3600)

for i in range(100):
  scheduler_reddit()

Change 3600 with the desired time in seconds.

Upvotes: 2

Related Questions