-
Hi everyone, This is from # YOUR CODE HERE
%%writefile get_data.py
import os
import zipfile
import requests
from pathlib import Path
# Let's check if the directory has already existed
def get_data(file_url):
# define pathes
data_path = Path('data')
image_path = data_path / file_url.split(r'/')[-1].split('.')[0]
if image_path.is_dir():
print(f'{str(image_path)} exists... skip dowload file...')
else:
print(f'{str(image_path)} does not exist, create a one')
image_path.mkdir(parents=True,exist_ok=True)
with open(data_path / file_url.split(r'/')[-1], 'wb') as f:
response = requests.get(file_url)
f.write(response.content)
print('Zip file donwloaded...')
with zipfile.ZipFile(data_path / file_url.split(r'/')[-1]) as zip_ref:
zip_ref.extractall(image_path)
print('Unzipping file...')
os.remove(data_path / file_url.split(r'/')[-1])
print('Your data is ready!')
train_dir = image_path / 'train'
test_dir = image_path / 'test'
return train_dir, test_dir
if __name__ == '__main__':
FILE_URL = 'https://github.com/mrdbourke/pytorch-deep-learning/raw/main/data/pizza_steak_sushi_20_percent.zip'
train_dir, test_dir = get_data(FILE_URL) This function worked well on a notebook, It can download the file and return import get_data
FILE_URL = 'https://github.com/mrdbourke/pytorch-deep-learning/raw/main/data/pizza_steak_sushi_20_percent.zip'
train_dir, test_dir = get_data.get_data(FILE_URL)
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @NoelTW , I'm not 100% sure why your code isn't working on your end. But I can confirm it works on my end. Perhaps try to remove For reference, my files are:
# YOUR CODE HERE
import os
import zipfile
import requests
from pathlib import Path
# Let's check if the directory has already existed
def get_data(file_url):
# define pathes
data_path = Path('data')
image_path = data_path / file_url.split(r'/')[-1].split('.')[0]
if image_path.is_dir():
print(f'{str(image_path)} exists... skip dowload file...')
else:
print(f'{str(image_path)} does not exist, create a one')
image_path.mkdir(parents=True,exist_ok=True)
with open(data_path / file_url.split(r'/')[-1], 'wb') as f:
response = requests.get(file_url)
f.write(response.content)
print('Zip file donwloaded...')
with zipfile.ZipFile(data_path / file_url.split(r'/')[-1]) as zip_ref:
zip_ref.extractall(image_path)
print('Unzipping file...')
os.remove(data_path / file_url.split(r'/')[-1])
print('Your data is ready!')
train_dir = image_path / 'train'
test_dir = image_path / 'test'
return train_dir, test_dir
if __name__ == '__main__':
FILE_URL = 'https://github.com/mrdbourke/pytorch-deep-learning/raw/main/data/pizza_steak_sushi_20_percent.zip'
train_dir, test_dir = get_data(FILE_URL)
from get_data import get_data
FILE_URL = 'https://github.com/mrdbourke/pytorch-deep-learning/raw/main/data/pizza_steak_sushi_20_percent.zip'
train_dir, test_dir = get_data(file_url=FILE_URL) Output: ![]() |
Beta Was this translation helpful? Give feedback.
Thank you @mrdbourke
I think I just figured out why my code didn't work.
Because once Colab imports the module, it keeps the first version and sets it as default.
It wouldn't replace overwritten version even when I tried to import again the newer version.
So I just solved this by restarting runtime.
A quick demo: