Skip to content

Commit 0025263

Browse files
Day 10 - Files - Create Read & Download
1 parent 8b09ed8 commit 0025263

File tree

19 files changed

+166
-0
lines changed

19 files changed

+166
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
In general, to create or retrieve a file on your local system you must use the `open` command. Open has a few different "modes" so let's go through them now.
2+
3+
## 1 Create a file with the `w` mode
4+
`w` means write as in write-mode. First let's define a random path to a filename that does not yet exist.
5+
```python
6+
to_save_dir = "/path/to/save/in/"
7+
filename = "myfilename.txt"
8+
full_path = to_save_dir + filename
9+
```
10+
Now let's `open()` that path so we can save it.
11+
12+
```python
13+
with open(full_path, 'w') as file_object:
14+
file_object.write("Hello world")
15+
```
16+
17+
If we don't want to use `with` we just have to explicitly `close` the `open` call.
18+
19+
```python
20+
file_object = open(full_path, 'w')
21+
file_object.write("Hello World")
22+
file_object.save()
23+
```
24+
25+
## 2 Open a file with the `r` mode
26+
`r` means write as in write-mode. First let's define a random path to a filename that already exists (we created it above).
27+
```python
28+
to_save_dir = "/path/to/save/in/"
29+
filename = "myfilename.txt"
30+
full_path = to_save_dir + filename
31+
```
32+
33+
```python
34+
with open(full_path, 'r') as file_object:
35+
contents = file_object.read()
36+
print(contents)
37+
```
38+
39+
If we don't want to use `with` we just have to explicitly `close` the `open` call.
40+
41+
```python
42+
file_object = open(full_path, 'r')
43+
contents = file_object.read()
44+
print(contents)
45+
contents.close()
46+
```
47+
48+
## 3 Download files
49+
50+
```python
51+
def download_file(url, directory, fname=None):
52+
if fname == None:
53+
fname = os.path.basename(url)
54+
dl_path = os.path.join(directory, fname)
55+
with requests.get(url, stream=True) as r:
56+
with open(dl_path, 'wb') as f:
57+
shutil.copyfileobj(r.raw, f)
58+
return new_dl_path
59+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
import requests
3+
import shutil
4+
from download_util import download_file
5+
THIS_FILE_PATH = os.path.abspath(__file__)
6+
BASE_DIR = os.path.dirname(THIS_FILE_PATH)
7+
DOWNLOADS_DIR = os.path.join(BASE_DIR, "downloads")
8+
os.makedirs(DOWNLOADS_DIR, exist_ok=True)
9+
10+
downloaded_img_path = os.path.join(DOWNLOADS_DIR, '1.jpg')
11+
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/db/Classic_view_of_a_cloudfree_Peyto_Lake%2C_Banff_National_Park%2C_Alberta%2C_Canada_%284110933448%29.jpg/330px-Classic_view_of_a_cloudfree_Peyto_Lake%2C_Banff_National_Park%2C_Alberta%2C_Canada_%284110933448%29.jpg"
12+
13+
# a smallish item
14+
r = requests.get(url, stream=True)
15+
r.raise_for_status() # 200
16+
with open(downloaded_img_path, 'wb') as f:
17+
f.write(r.content)
18+
19+
20+
# dl_filename = os.path.basename(url)
21+
# new_dl_path = os.path.join(DOWNLOADS_DIR, dl_filename)
22+
# with requests.get(url, stream=True) as r:
23+
# with open(new_dl_path, 'wb') as file_obj:
24+
# shutil.copyfileobj(r.raw, file_obj)
25+
26+
27+
download_file(url, DOWNLOADS_DIR)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
import requests
3+
import shutil
4+
5+
def download_file(url, directory, fname=None):
6+
if fname == None:
7+
fname = os.path.basename(url)
8+
dl_path = os.path.join(directory, fname)
9+
with requests.get(url, stream=True) as r:
10+
with open(dl_path, 'wb') as f:
11+
shutil.copyfileobj(r.raw, f)
12+
return new_dl_path
13+
14+
15+
16+
def download_file_slower(url):
17+
local_filename = url.split('/')[-1]
18+
# NOTE the stream=True parameter below
19+
with requests.get(url, stream=True) as r:
20+
r.raise_for_status()
21+
with open(local_filename, 'wb') as f:
22+
for chunk in r.iter_content(chunk_size=8192):
23+
if chunk: # filter out keep-alive new chunks
24+
f.write(chunk)
25+
# f.flush()
26+
return local_filename
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
3+
this_file_path = os.path.abspath(__file__)
4+
# print(this_file_path)
5+
BASE_DIR = os.path.dirname(this_file_path)
6+
ENTIRE_PROJECT_DIR = os.path.dirname(BASE_DIR)
7+
# print(BASE_DIR, ENTIRE_PROJECT_DIR)
8+
9+
email_txt = os.path.join(BASE_DIR, "templates", "email.txt")
10+
11+
content = ""
12+
13+
with open(email_txt, 'r') as f:
14+
content = f.read()
15+
16+
17+
print(content.format(name='Justin'))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world again
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
3+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
4+
files_dir = os.path.join(BASE_DIR, "images")
5+
6+
# if not os.path.exists(files_dir):
7+
# print("This is not a valid path")
8+
9+
os.makedirs(files_dir, exist_ok=True)
10+
11+
my_images = range(0, 12)
12+
13+
for i in my_images:
14+
fname = f"{i}.txt"
15+
file_path = os.path.join(files_dir, fname)
16+
if os.path.exists(file_path):
17+
print(f"skipped {fname}")
18+
continue
19+
with open(file_path, 'w') as f:
20+
f.write("Hello world")
21+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Hello {name},
2+
3+
How's it going?

0 commit comments

Comments
 (0)