I have a Python 3 script that I am writing to do three things:
1) Determine which Retrosheets data files are supposed to be downloaded 2) Create wget commands to retrieve the files and download them 3) Unzip the files after they have been downloaded.
When testing each function in the Python Console, I have no problems. But, when I try to do everything automatically, I get the following output:
Start Decade: 1930
End Decade: 1950
Creating wget commands...
Commands created...
Downloaded 3 files.
Unzipping files...
Traceback (most recent call last):
File "import_pbp.py", line 54, in <module>
unzip_data(decade_files)
File "import_pbp.py", line 39, in unzip_data
with zipfile.ZipFile('zip' + file, 'r') as zip_ref:
File "/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5 /lib/python3.5/zipfile.py", line 1009, in __init__
self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: 'zip1930seve.zip'
The files are downloaded after this output to the console. This would seem to indicate that the unzip function is running before the files are downloaded. How do I make sure that my files are downloaded before the unzip function is called? Code below:
Download function:
# define function to execute the download commands
def download_data(commands):
for command in commands:
os.popen(command)
print('Downloaded ' + str(len(commands)) + ' files.')
Unzip Function:
# Unzip the data files into the 'unparsed' folder.
def unzip_data(file_list):
print('Unzipping files...')
for file in file_list:
with zipfile.ZipFile('zip' + file, 'r') as zip_ref:
zip_ref.extractall('unparsed/')
print(file + ' unzipped')
print('All files unzipped...')
EDIT: I looked at the response in this thread but it didn't quite explain what I needed like tdelaney did below. They are similar, but for my purposes, different. Especially since that question is 6 years old and I'm guessing there may have been significant changes to the language since then.
EDIT 2: Removed non-essential code to shorten the post.