-
Notifications
You must be signed in to change notification settings - Fork 0
/
LAB031.py
51 lines (37 loc) · 1.47 KB
/
LAB031.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import requests
import os
import shutil
def save_url_to_file(url, file_path):
r = requests.get(url, stream=True)
with open(file_path, "wb") as f:
f.write(r.content)
url = 'sdaasx'
dir = r'E:\dzd07'
tmpfile = 'download.tmp'
file = 'spis.html'
tmpfile_path = os.path.join(dir, tmpfile)
file_path = os.path.join(dir, file)
try:
if os.path.exists(tmpfile_path):
print('Removing {}'.format(tmpfile_path))
os.remove(tmpfile_path)
print('Downloading url {}'.format(url))
save_url_to_file(url, tmpfile_path)
print('Copying file {} {}'.format(tmpfile_path, file_path))
shutil.copy(tmpfile_path, file_path)
except requests.exceptions.ConnectionError:
print('ten błąd łatwo sprowokujesz wpisując nieprawidłowy adres URL {}'.format(e))
except PermissionError:
print('ten błąd uzyskasz zaznaczając atrybut "tylko do odczytu" dla pliku spis.html{}'.format(e))
except FileNotFoundError:
print('może się pojawić w trakcie prób, gdy plik download.tmp nie będzie istniał, a wykonywać będzie się instrukcja kopiowania plików{}'.format(e))
except Exception as e:
print('Error downloading the URL {}'.format(url))
print('Error details: {}'.format(e))
else:
print('URL downloaded successfully {}'.format(file))
finally:
if os.path.exists(tmpfile_path):
print('Final removal of the file {}'.format(tmpfile_path))
os.remove(tmpfile_path)
print('DONE!')