Unzip all ziped files in another working directory - python

I would like t write the script where all zip files in the folder would be extracted in another folder. I found a helpful answer here: Unzip all zipped files in a folder to that same folder using Python 2.7.5
I slightly edited the code from the answers there as listed down below
import zipfile36 as zipfile
working_dir = r"C:\Users\Tim\Desktop\Tim\Faks\NMG\Python\Python Scripting for Geoprocessing Workflows\PythonGP\PythonGP\Scripts" ###### LAHKO MENJAŠ POT ######
goal_dir= r"C:\Users\Tim\Desktop\Tim\Faks\NMG\Python\Python Scripting for Geoprocessing Workflows\PythonGP\PythonGP\Scripts\Ekstrahirano"
extension = ".zip"
for item in os.listdir(working_dir): # loop through items in dir
if item.endswith(extension): # check for ".zip" extension
file_name = os.path.abspath(item) # get full path of files
zip_ref = zipfile.ZipFile(file_name) # create zipfile object
zip_ref.extractall(goal_dir) # extract file to dir
zip_ref.close() # close file
os.remove(file_name) # delete zipped file
When running, I am constantly receiving this error
Traceback (most recent call last):
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\IPython\core\interactiveshell.py", line 3437, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-98-d46cd220b77f>", line 8, in <module>
zip_ref = zipfile.ZipFile(file_name) # create zipfile object
File "C:\Users\Tim\AppData\Roaming\Python\Python39\site-packages\zipfile36.py", line 1100, in __init__
self._RealGetContents()
File "C:\Users\Tim\AppData\Roaming\Python\Python39\site-packages\zipfile36.py", line 1167, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile36.BadZipFile: File is not a zip file
What am I missing?

Related

Combining PDFs errno 2 no such file or directory even the file exists python

I'm combining numerous PDF files in Python and getting the error 'errno 2 nno such file or directory' despite the fact that the file exists.
I tried to display the PDF files just to show that the PDF file exists
import os
from PyPDF2 import PdfFileMerger
merger = PdfFileMerger()
source_dir = os.getcwd() + '/Combined PDF'
for items in os.listdir(source_dir):
if items.endswith('.pdf'):
print(items)
#merger.append(items)
#merger.write('./Combined PDF/CombinedPDF.pdf')
#merger.close()
Result
PS C:\Users\RVC\Desktop\Python> & C:/Users/RVC/AppData/Local/Microsoft/WindowsApps/python3.9.exe "c:/Users/RVC/Desktop/Python/Combined PDF/test.py"
PDF 1.pdf
PDF 2.pdf
PDF 3.pdf
PDF 4.pdf
But when I removed the comment # and execute it failed
Traceback (most recent call last):
File "c:\Users\RVC\Desktop\Python\Combined PDF\test.py", line 11, in <module>
merger.append(items)
File "C:\Users\RVC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\PyPDF2\merger.py", line 203, in append
self.merge(len(self.pages), fileobj, bookmark, pages, import_bookmarks)
File "C:\Users\RVC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\PyPDF2\merger.py", line 114, in merge
fileobj = file(fileobj, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'PDF 1.pdf'
PS C:\Users\RVC\Desktop\Python>
You are appending PDF 1.pdf etc. which means merger expects those files to be in cwd.

Python - Can't locate downloaded file to unzip

Using selenium, I was able to automate the download of a zip file and save it to a specified directory. When I try to unzip the file, however, I hit a snag where I can't seem to locate the recently downloaded file. If it helps, this is the block of code related to the downloading and unzipping process:
# Click on Map Link
driver.find_element_by_css_selector("input.linksubmit[value=\"▸ Map\"]").click()
# Download Data
driver.find_element_by_xpath('//*[#id="buttons"]/a[4]/img').click()
# Locate recently downloaded file
path = 'C:/.../Download'
list = os.listdir(path)
time_sorted_list = sorted(list, key=os.path.getmtime)
file_name = time_sorted_list[len(time_sorted_list)-1]
Specifically, this is my error:
Traceback (most recent call last):
File "C:\Users\...\AppData\Local\Continuum\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-89-3f1d00dac284>", line 3, in <module>
time_sorted_list = sorted(list, key=os.path.getmtime)
File "C:\Users\...\AppData\Local\Continuum\Anaconda3\lib\genericpath.py", line 55, in getmtime
return os.stat(filename).st_mtime
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'grid-m1b566d31a87cba1379e113bb93fdb61d5be5b128.zip'
I tried troubleshooting the code by deleting it and placing another file in the directory, and I was able to find the random file, but not the recently downloaded file. Can anyone tell me what's going on here?
First of all, do not use list for a variable name. That hides the list constructor from being readily available to use somewhere else in your program. Second, os.listdir does not return the full path of the files in that directory. If you want the full path, there are two things you can do:
You can use os.path.join:
import zipfile
path = 'C:/.../Download'
file_list = [os.path.join(path, f) for f in os.listdir(path)]
time_sorted_list = sorted(file_list, key=os.path.getmtime)
file_name = time_sorted_list[-1]
myzip = zipfile.ZipFile(file_name)
for contained_file in myzip.namelist():
if all(n in contained_file.lower() for n in ('corn', 'irrigation', 'high', 'brazil')):
with myzip.open(contained_file) as f:
# save data to a CSV file
You can also use the glob function from the glob module:
from glob import glob
import zipfile
path = 'C:/.../Download'
file_list = glob(path+"/*")
time_sorted_list = sorted(file_list, key=os.path.getmtime)
file_name = time_sorted_list[-1]
myzip = zipfile.ZipFile(file_name)
for contained_file in myzip.namelist():
if all(n in contained_file.lower() for n in ('corn', 'irrigation', 'high', 'brazil')):
with myzip.open(contained_file) as f:
# save data in a CSV file
Either should work.

Python: TarFile.open No such file or directory

So I'm fairly new to python and i'm writing a script that needs to untar a file. I use this simple function.
def untar(source_filename, dest_dir):
for f in os.listdir():
print(f)
if(source_filename.endswith("tar.gz") or source_filename.endswith(".tar")):
tar = tarfile.open(source_filename)
tar.extractall(dest_dir)
tar.close()
else:
raise Exception("Could not retrieve .depends for that file.")
I added the initial for loop for debugging purposes. When I invoke it, it prints out the name of the file i need in the current working directory meaning that it does exist. Here is the whole output.
dep.tar.gz
Traceback (most recent call last):
File "init.py", line 70, in <module>
untar('dep.tar.gz', ".")
File "init.py", line 17, in untar
tar = tarfile.open(source_filename)
File "/usr/lib/python3.4/tarfile.py", line 1548, in open
return func(name, "r", fileobj, **kwargs)
File "/usr/lib/python3.4/tarfile.py", line 1646, in bz2open
compresslevel=compresslevel)
File "/usr/lib/python3.4/bz2.py", line 102, in __init__
self._fp = _builtin_open(filename, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'dep.tar.gz'
Can someone tell me how it can see the file in the working directory, and then suddenly not be able to see the file in the working directory?
The program I was using to create the tar placed a space at the beginning of the filename. So python was looking for 'dep.tar.gz' and the actual filename was ' dep.tar.gz'. ty #Ben
TIL - filenames can start with spaces.

Create folders based on file names and copy files to them in python

I have many files in this folder structure:
test[dir]
-test1 - 123.avi
-video[dir]
-test2 - 123.avi
I want to create folders based based on file names (e.g. test1, test2) in the target dir and move files to respective folders.
I tried this based on code from another thread:
#!/usr/bin/env python
import os, shutil
src = "/home/koogee/Code/test"
dest = "/home/koogee/Downloads"
for dirpath, dirs, files in os.walk(src):
for file in files:
if not file.endswith('.part'):
Dir = file.split("-")[0]
newDir = os.path.join(dest, Dir)
if (not os.path.exists(newDir)):
os.mkdir(newDir)
shutil.move(file, newDir)
I get this error:
Traceback (most recent call last):
File "<stdin>", line 8, in <module>
File "/usr/lib/python2.7/shutil.py", line 299, in move
copy2(src, real_dst)
File "/usr/lib/python2.7/shutil.py", line 128, in copy2
copyfile(src, dst)
File "/usr/lib/python2.7/shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'test1'
What is weird is that there is a folder created in /home/koogee/Downloads named 'test1'
When you try to do shutil.move(), your file variable is just the file name with no context of the directory, so it is looking for a file of that name in the script's current directory.
To get an absolute path, use os.path.join(dirpath, file) as the source:
shutil.move(os.path.join(dirpath, file), newDir)

How to move all .log and .txt files to a new folder

I'm having trouble figuring out how to move all .log and .txt files in a certain folder and it's subdirectories to a new folder. I understand how to move one file with shutil. But, I tried to use a loop, unsuccessfully, to move all. Can someone help me with this? Thanks ....
import os, os.path
import re
def print_tgzLogs (arg, dir, files):
for file in files:
path = os.path.join (dir, file)
path = os.path.normcase (path)
defaultFolder = "Log_Text_Files"
if not defaultFolder.endswith(':') and not os.path.exists('c:\\Extracted\Log_Text_Files'):
os.mkdir('C:\\Extracted\\Log_Text_Files')
if re.search(r".*\.txt$", path) or re.search(r".*\.log$", path):
os.rename(path, 'C:\\Extracted\\Log_Text_Files')
print path
os.path.walk('C:\\Extracted\\storage', print_tgzLogs, 0)
Below is the trace back error:
Traceback (most recent call last):
File "C:\SQA_log\scan.py", line 20, in <module>
os.path.walk('C:\\Extracted\\storage', print_tgzLogs, 0)
File "C:\Python27\lib\ntpath.py", line 263, in walk
walk(name, func, arg)
File "C:\Python27\lib\ntpath.py", line 263, in walk
walk(name, func, arg)
File "C:\Python27\lib\ntpath.py", line 263, in walk
walk(name, func, arg)
File "C:\Python27\lib\ntpath.py", line 259, in walk
func(arg, top, names)
File "C:\SQA_log\scan.py", line 16, in print_tgzLogs
os.rename(path, 'C:\\Extracted\\Log_Text_Files')
WindowsError: [Error 183] Cannot create a file when that file already exists
According to the traceback, the log-files are already existing. The Python docs to the os.rename say:
On Windows, if dst already exists, OSError will be raised [...].
Now you can either:
delete the files manually or
delete the files automatically using os.remove(path)
If you want the files to be automatically deleted, the code would look like this (notice that I replaced your regular expression with the python endswith as suggested by utdemir):
import os, os.path
def print_tgzLogs (arg, dir, files):
for file in files:
path = os.path.join (dir, file)
path = os.path.normcase (path)
defaultFolder = "Log_Text_Files"
if not defaultFolder.endswith(':') and not os.path.exists('c:\\Extracted\Log_Text_Files'):
os.mkdir('C:\\Extracted\\Log_Text_Files')
if path.endswith(".txt") or path.endswith(".log"):
if os.path.exists('C:\\Extracted\\Log_Text_Files\\%s' % file):
os.remove('C:\\Extracted\\Log_Text_Files\\%s' % file)
os.rename(path, 'C:\\Extracted\\Log_Text_Files\\%s' % file)
print path
os.path.walk('C:\\Extracted\\storage', print_tgzLogs, 0)
It looks like are trying to use
os.rename(path, 'C:\\Extracted\\Log_Text_Files')
to move the file path into the directory C:\Extracted\Log_Text_Files, but rename doesn't work like this: it's going to try to make a new file named C:\Extracted\Log_Text_Files. You probably want something more like this:
os.rename(path, os.path.join('C:\\Extracted\\Log_Text_Files',os.path.basename(path))

Categories