title: Trying to loop up and down directories for my bot program but struggling with it
body: <p>Ok so a bit of introduction. I am trying to create a bot that scrapes the images and posts of reddit and tweets them out. This is not my full project but its pretty much all the relevant stuff. </p>
  
  <p>So the thing is I need to download the images into a directory and ive called that directory 'pics' but I also need to log the reddit post ID's (which I do with a different function not displayed here) into a text file and I need to read through it to check if its already been tweeted. This is what the already_tweeted function does.</p>
  
  <p>Here is the problem, on my first loop, It will read through the file (which is empty at the moment) and then move on to using the get_image funcition. I need to change the current directory to the image directory so I can store the images which I do (os.chdir(img_dir)). Now the directory is changed and when I go back to read the oldies.txt file it says it doesnt exist since It is in the original directory and not the pics directory.</p>
  
  <p>So What I need to do is go up to the original directory after I have downloaded the images into the 'pics' directory and I am completely stuck on that. </p>
  
  <pre><code>import time
  import os
  from bs4 import BeautifulSoup as bs
  import requests
  import praw
  import tweepy
  
  def tweet_creator(subreddit_info):
      '''Goes through posts on reddit and extracts a shortened link, title &amp; ID'''
      post_links = [] #list to store our links
      post_titles = [] #list to store our titles
      post_ids = [] #list to store our id's
      post_imgs = []
      print("[bot] extracting posts from sub-reddit")
  
      for submission in subreddit_info.new(limit=5):
          if not already_tweeted(submission.id):
              post_titles.append(submission.title)
              post_links.append(submission.shortlink)
              post_ids.append(submission.id)
              post_imgs = get_image(submission.url)
              print(post_imgs)
  
          else:
              print("Already Tweeted")
      return post_links, post_titles, post_ids, post_imgs
  
  def already_tweeted(post_id):
      '''reads through our .txt file and determines if tweet has already been posted'''
      found = 0
  
      print(os.getcwd())
      with open(posted_reddit_ids, 'r') as f:
          for line in f:
              if post_id in line:
                  found = 1
                  break
      return found
  
  def get_image(img_url):
      url = img_url
      r = requests.get(url, headers = {'User-Agent' : 'reddit Twitter tool monitoring (by /u/RivianJourneyMan)'})
      data = r.text
      soup = bs(data, 'lxml')
  
      image_tags = soup.findAll('img')
  
      os.chdir(img_dir)
      x = 0
      mylist = []
      for image in image_tags:
              url = image['src']
              source = requests.get(url, stream = True)
              if source.status_code == 200:
                  img_file = img_dir + str(x) + '.jpg'
                  with open(img_file, 'wb') as f:
                      f.write(requests.get(url).content)
                      mylist.append(img_file)
                      f.close()
                      x += 1
                      return img_file
              else:
                  mylist.append(None)
      return mylist
  </code></pre>
  
  <p>Once again this is not my full code, only the relevant parts but this is what it outputs when I run it. As you can see in the alread_tweated function, I asked it to print out what directory I am in as I am looping to make it easier to see the problem. </p>
  
  <pre><code>[bot] Setting up connection with reddit
  [bot] extracting posts from sub-reddit
  C:\Users\ali\PycharmProjects\cyberbot
  pics0.jpg
  C:\Users\ali\PycharmProjects\cyberbot\pics
  Traceback (most recent call last):
    File "C:/Users/ali/PycharmProjects/cyberbot/rtbot_with_img2.py", line 154, in &lt;module&gt;
      main()
    File "C:/Users/ali/PycharmProjects/cyberbot/rtbot_with_img2.py", line 150, in main
      post_links, post_titles, post_ids, post_imgs = tweet_creator(subreddit)
    File "C:/Users/ali/PycharmProjects/cyberbot/rtbot_with_img2.py", line 58, in tweet_creator
      if not already_tweeted(submission.id):
    File "C:/Users/ali/PycharmProjects/cyberbot/rtbot_with_img2.py", line 81, in already_tweeted
      with open(posted_reddit_ids, 'r') as f:
  FileNotFoundError: [Errno 2] No such file or directory: 'oldies.txt'
  </code></pre>
  
  <p>Now I have been looking everywhere and trying as many solutions to go back to the original directory but none of them work. I've tried os.chdir('..') and I put it just above the 'return mylist[]' but that didn't work. I pretty much want to go into the 'pics' directory to download my images and then I want to go back to my original directory which is the 'cyberbot' one so that I can read through my oldies.txt file to see if I already tweeted the post</p>
  
  <p><a href="https://i.stack.imgur.com/8YMKG.jpg" rel="nofollow noreferrer">Perhaps this picture of how my directories and files will help</a></p>