close

How to save a file to a specific directory in python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to save a file to a specific directory in python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to save a file to a specific directory in python?

  1. How to save a file to a specific directory in python?

    You can just give open a full file path or a relative file path
    r = requests.get(url) with open(r'C:\path\to\save\file_name.pdf', 'wb') as f: f.write(r.content)

  2. save a file to a specific directory in python

    You can just give open a full file path or a relative file path
    r = requests.get(url) with open(r'C:\path\to\save\file_name.pdf', 'wb') as f: f.write(r.content)

Method 1

Or if in Linux, try:

# To save to an absolute path.
r = requests.get(url)  
with open('/path/I/want/to/save/file/to/file_name.pdf', 'wb') as f:
    f.write(r.content)


# To save to a relative path.
r = requests.get(url)  
with open('folder1/folder2/file_name.pdf', 'wb') as f:
    f.write(r.content)

Method 2

You can just give open a full file path or a relative file path

r = requests.get(url)  
with open(r'C:\path\to\save\file_name.pdf', 'wb') as f:
    f.write(r.content)

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read