File DL, byte value and delete in Python3

DL from the URL to check the byte value and the file will be overwhelmed, so just put it in the trash * ⌒ ヾ (* ´- `) programming.

(Addition) This time, since the URL destination is only the URL saved in the DB, we took the form of saving it locally. It is said that it is not necessary to save it in the end, which will be described later.

Source


#Import library
import urllib.request
import os.path
 
# URL,Specify the path to save
url = "https://xxxxxxx.co.jp/1234567.mp4"
save_name = "sample.mp4"
 
#to download
mem = urllib.request.urlopen(url).read()
 
#Save to file
with open(save_name, mode="wb") as f:
    f.write(mem)

#Earn bytes
ret = os.path.getsize(save_name)
print(ret)

#Delete file
os.remove(save_name)

~~ I wanted to get the byte value from the downloaded thing in the memory, but it seemed to be useless unless I dropped it locally, so I wonder if there is another good way. ~~

2019/11/20 postscript

The file size can be obtained from the data in the memory.

mem = urllib.request.urlopen(url).read()
print(len(mem))

Apart from that, if you use the HEAD method in urllib.request, you can get only the header information and get the file size. (From comments)

I thought I had to study properly because it was my first time without even looking at the introductory book on Python. Reflection (´ ・ ω ・ `)

Recommended Posts

File DL, byte value and delete in Python3
Copy file and rewrite cell value @python
Write O_SYNC file in C and Python
File operations in Python
File processing in Python
File operations in Python
Download the file in Python
Shapley value calculation in Python
Stack and Queue in Python
Unittest and CI in Python
Get the MIME type in Python and determine the file format
File / folder path manipulation in Python
Difference between list () and [] in Python
Difference between == and is in python
Sorting algorithm and implementation in Python
Save the binary file in Python
Linebot creation & file sharing in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Create a binary file in Python
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Python CSV file reading and writing
Hashing data in R and Python
Function synthesis and application in Python
Python memo ① Folder and file operations
Export and output files in Python
ORC, Parquet file operations in Python
Reverse Hiragana and Katakana in Python2.7
Delete multiple elements in python list
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
File open function in Python3 (difference between open and codecs.open and speed comparison)
Overlapping regular expressions in Python and Java
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
Exclusive control with lock file in Python
Differences between Ruby and Python in scope
AM modulation and demodulation in Python Part 2
difference between statements (statements) and expressions (expressions) in Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Implementation module "deque" in queue and Python
Line graphs and scale lines in python
Implement FIR filters in Python and C
Differences in syntax between Python and Java
Check and receive Serial port in Python (Port check)
Convert psd file to png in Python
Search and play YouTube videos in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Read the file line by line in Python
Dealing with "years and months" in Python
Read the file line by line in Python
Exclusive file access between processes in Python
Read and write JSON files in Python
The file name was bad in Python and I was addicted to import