Home > Enterprise >  How to re-direct output path in python
How to re-direct output path in python

Time:07-26

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)

CodePudding user response:

An even better way

file_path = os.path.realpath(__file__)
output_files = file_path.replace('your_file_name.py','yourfolder/destination')
os.chdir(output_files)

CodePudding user response:

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)
  • Related