Skip to content
Advertisement

How to re-direct output path in python

with open('URLS/Gibiru_urls.txt', 'r') as urls:
    for url in urls.readlines():
        url = url.rstrip("n")
        download_url(url)

I want to read urls from .txt file in one directory (Root/URLS/Gibiru_urls.txt) and output into another directory (Root/Images/Gibiru_pics). My python file is located in (Root)

def download_url(file_url):
  print("downloading: ",file_url)

  file_name_start_pos = file_url.rfind("/") + 1
  file_name = file_url[file_name_start_pos:]
  os.system("cd Images/Gibiru_pics")

  r = requests.get(file_url, stream=True)
  if r.status_code == requests.codes.ok:
    with open(file_name, 'wb') as f:
      for data in r:
        f.write(data)

Advertisement

Answer

I was able to re-direct. It was the os.chdir() method I was looking for.

def Gibiru():
    output_dir = '/multiple_image_gathering-main/Images/Gibiru_pics'
    with open('URLS/Gibiru_urls.txt', 'r') as urls:
        for url in urls.readlines():
            url = url.rstrip("n")
            download_url(url, output_dir)

def download_url(file_url, output_dir):
    os.chdir(output_dir)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement