PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 28, 2022

[FIXED] How to re-direct output path in python

 July 28, 2022     download, file, image, python, url     No comments   

Issue

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)

Solution

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)


Answered By - AnxiousLuna
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing