How to open and load a JSON file inside a .zip archive? - python

So I have a python script I run in a directory which contains .zip files and these zip files all have JSON files which I want to load. With my current method I get an error of 'No such file or directory' when I try to 'open(filename)' I assume this is because namelist() doesn't actually enter the .zip directory. How can I load the JSON file inside this zip archive once I confirm that it is indeed a .zip archive?
import zipfile
import json
import os
myList = []
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
if f.endswith('.zip'):
with zipfile.ZipFile(f) as myzip:
for filename in myzip.namelist():
if filename.endswith('.json'):
g = open(filename)
data = json.load(g)
#do stuff with g and myList

Related

Save outputs to new directory in Python

I am trying to identify all .kml in a specific directory and save them into a new directory. Is this possible? I'm able to print the file path but I would like to use Python to copy those files to a new directory.
Here is my code so far:
import os
# traverse whole directory
for root, dirs, files in os.walk(r'C:\Users\file_path_here'):
# select file name
for file in files:
# check the extension of files
if file.endswith('.kml'):
# print whole path of files
print(os.path.join(root, file))
Try this:
import os
# traverse whole directory
for root, dirs, files in os.walk(r'C:\Users\file_path_here'):
# select file name
for each_file in files:
# check the extension of files
if each_file.endswith('.kml'):
# print whole path of files
print(os.path.join(root, file))
kml_file = open(each_file, "r")
content = kml_file.read()
file.close()
with open('newfile.kml', 'w') as f:
f.write(content)

Walk directories and remove file extensions

I'm trying to remove all the outlook .ost and .nst files from the user's folder on a network PC, as well as I'm trying to get it to write what files were removed into a CSV file.
I'm able to get it to find all the files in the directory and write it to a CSV file but when I try to remove the files with os.remove it doesn't seem to run, I hashed it out for the time being.
I added in the try and except, to skip the files that are in use.
import os
import sys
sys.stdout = open("output_file.csv", "w")
try:
for rootDir, subdir, files in os.walk("//network_pc_name/c$/Users"):
for filenames in files:
if filenames.endswith((".nst",".ost")):
foundfiles = os.path.join(rootDir, filenames)
#os.remove(os.path.join(rootDir, filenames))
print(foundfiles)
except:
pass
sys.stdout.close()
I made some change to the script as suggested and it appears to run alot quicker, however, I can't seem to figure out how to ignore files which are in use.
I switched the files extensions to .xlsx and .txt files to simulate the .xlsx file being open receiving the permissions error and to see if the script would continue to run and remove the .txt file.
I got the following error:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: '//DESKTOP-HRLS19N/c$/globtest\Book1.xlsx
import glob
import os
files = [i for i in glob.glob("//DESKTOP-HRLS19N/c$/globtest/**", recursive = True) if i.endswith((".xlsx",".txt"))]
[os.remove(f) for f in files]
with open("output_file.csv", "w") as f:
f.writelines("\n".join(files))
In my experience glob is much easier:
print([i for i in glob.glob("//network_pc_name/c$/Users/**", recursive=True) if i.endswith((".nst", ".ost"))])
Assuming that prints out the files you're expecting:
files = [i for i in glob.glob("//network_pc_name/c$/Users/**", recursive=True) if i.endswith((".nst", ".ost"))]
removed_files = []
for file in files:
try:
size = os.path.getsize(file)
os.remove(file)
removed_files.append(file + " Bytes: " + size)
except Exception as e:
print("Could not remove file: " + file)
with open("output_file.csv", "w") as f:
f.writelines("\n".join(removed_files))

The directory name is invalid for my temporary folder error?

I am trying to write a nested try-except statement that opens .zip, .gz, and .tar folder, and also regular. txt files. I am downloading these files from the internet, and extract them to a temporary folder. My code works for the .zip, .gz, and .tar folders, but it says my directory name is invalid for the regular .txt files I'm trying to extract to the temporary folder. Here is my code.
def function(file_download_url):
urllib.request.urlopen(file_download_url) #download the zipped file
dirpath = tempfile.mkdtemp() #Generate a temporary directory
#Download the URL as an temporary file that has to be deleted later on
with urllib.request.urlopen(file_download_url) as response:
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
shutil.copyfileobj(response, tmp_file)
print(tmp_file.name) #To display the name of the temporary file created
#Try-Except statement that extracts compressed files to a temporary directory generated
try:
#If it's a .zip file
with ZipFile(tmp_file.name) as my_zip_file: #Open up the downloaded zipped file
my_zip_file.extractall(dirpath) #extract the support bundle to the temporary directory created
path = dirpath #Make the temporary directory the path for searching
except:
try:
#If it's a .gz or .tar file
with tarfile.open(tmp_file.name) as tar:
tar.extractall(dirpath)
path = dirpath
print(path)
except:
#If it's just a .txt or .log file
source = tmp_file.name
dest = dirpath
files = os.listdir(source) #Here is where the error "The directory name is invalid" occurs
for f in files:
shutil.move(source, dest)
The problem line is files = os.listdir(source), you cannot perform a listdir
operation on a single file. You should instead skip right to the move operation:
...
except:
#If it's just a .txt or .log file
source = tmp_file.name
dest = dirpath
shutil.move(source, dest)

Compress all files in a folder with python?

this code takes a bunch of files in a folder (based on the file name), zips them into bz2 and adds them into a tar file. Is there a way I can modify this to only compress the files into bz2 (or gzip)? I do not want to have to deal with having them packaged into a tar. I just want to go through each file in a directory and compress it.
import os
from glob import glob
import tarfile
os.chdir(r'C:\Documents\FTP\\')
compression = "w:bz2"
extension = '.tar.bz2'
filename = 'survey_'
filetype = 'survey_report_*.csv'
tarname = saveloc+filename+extension
files = glob(filetype)
tar = tarfile.open(tarname, compression)
for file in files:
if file not in tarname:
print('Packaging file:', file)
tar.add(file)
tar.close()
EDIT:
This code seems to work for some files, but for other ones it makes them 1kb and when I open it there are just some random characters. Any suggestions?
import bz2
import os
location = r'C:\Users\Documents\FTP\\'
os.chdir(location)
filelist = os.listdir(location)
for file in filelist:
data = open(file).read()
try:
output = bz2.BZ2File(file + '.bz2', 'wb')
output.write(data)
finally:
output.close()

PYTHON - Zip each file in directory independently

I've been looking through and have tried a few different codes without results. What I'm trying to do is zip each file in a subdirectory/folder independently.
Ex:
FileName.prj
FileName.dwg
FileName.mp3
Each as it's own .zip
Thanks!
Try this
import os
import zipfile
folder = "/tmp/in"
dest_folder = "/tmp/out"
l = [os.path.join(folder, fname) for fname in os.listdir(folder)]
os.chdir(folder)
for f in l:
f_name = f[f.rfind("/")+1:]+".zip"
z = zipfile.ZipFile(f_name, 'w')
z.write(f_name[:f_name.rfind(".zip")])
os.rename(folder+"/"+f_name, dest_folder+"/"+f_name)
where folder is you folder that contains files you want to zip and dest_folder is folder where the zip files will be written.

Categories

Resources