read a zipfile without knowing file name in python - python

I have a zip file in a directory. I want to read out the contents of the zipfile and after this move its content to other directories to auto sort my zipped files. thing is, the name of these zipfiles will change everytime.
I learned about glob being able to handle the asterix for this normally, but it doesn't work with the zipfile import.
I tried:
from zipfile import ZipFile
from glob import glob
path_to_zip = glob(r"C:\me\Python\map_dumper\*.zip")
with ZipFile(path_to_zip, "r") as read_zip:
ZipFile.namelist()
This gives me an AttributeError:'list' object has no attribute 'seek'
anyone know of another solution for this?

glob.glob gives you list of filenames compliant with what you specify, for example
glob(r"C:\me\Python\map_dumper\*.zip")
is list of all zip files inside map_dumper catalog, you might use for loop to apply your action to every such file following way
from zipfile import ZipFile
from glob import glob
files_list = glob(r"C:\me\Python\map_dumper\*.zip")
for path_to_zip in files_list:
with ZipFile(path_to_zip, "r") as read_zip:
ZipFile.namelist()

Related

Import file of semi-known name to pandas and return filename

I'm writing a script to import a csv using pandas, then upload it to a SQL server. It all works when I have a test file with one name, but that won't be the case in production. I need to figure out how to import with only a semi-known filename (the filename will always follow the same convention, but there will be differences in the filenames such as dates/times). I should, however, note that there will only ever be one csv file in the folder it's checking. I have tried the wildcard in the import path, but that didn't work.
After it's imported, I also then need to return the filename.
Thanks!
Look into the OS module:
import os
files = os.listdir("./")
csv_files = [filename for filename in files if filename.endswith(".csv")]
csv_files is a list with all the files that ends with .csv

How to transfer multiple files from sub directories to a single path folder using python?

I have a list of names(all unique) of Wav files-
2003211085_2003211078_ffc0d543799a2984c60c581d.wav
2003214817_2003214800_92720fb19bf9216c2f160733.wav
2003233142_2003233136_8c42d206701830dff6032d41.wav
2003256235_2003256218_4e71bf77b0ffb907990d2e30.wav
2003276239_2003276196_dad6aff70f37817fcd75ffb8.wav
2003352182_2003352170_b1f2990d5f867408cc39c445.wav
There is a directory called \019\Recordings where all of these files are located under various subfolders.
I want to write a python app that pulls these wav files based on their unique name from all these subfolders and places them into a single target folder.
Im new to python and tried using -
import glob, os
import shutil
target_list_of_wav_names = ["2003211085_2003211078_ffc0d543799a2984c60c581d.wav",
"2003214817_2003214800_92720fb19bf9216c2f160733.wav",
"2003233142_2003233136_8c42d206701830dff6032d41.wav"
"2003352182_2003352170_b1f2990d5f867408cc39c445.wav"]
for file in glob.glob('//19/Recordings*.wav', recursive=True):
print(file)
if file in target_list_of_wav_names:
shutil.move(file, "C:/Users/ivd/Desktop/autotranscribe"+file)
But the files do not reflect in the target folder
How can i fix this?
glob is just a utility to find files based on a wildcard. It returns the string of the files that match your query.
So you'll still need to actually move the file with another function.
you could use os.rename or shutil.move to move it
for file in glob.glob("*.wav"):
os.rename(file, f'destinationfolder/{file}')
import glob, os
import shutil
target_list_of_wav_names = ['example_wav1.wav','example_wav2.wav',...... etc]
for file in glob.glob('/019/Recordings/*.wav', recursive=True):
print(file)
if file in target_list_of_wav_names:
shutil.move(file, "/mydir/"+file)

Passing variable to zipfile NAME

I want to assign to my zipfile from a variable named file_path, how I can do that? I tried with the code below, and this line is the problem:
with ZipFile('%s.zip' %(file_path,),'w') as zip:
Nothing happens; I get no errors just Python doesn't create the .zip file. Here is all the code.
import os
from zipfile import ZipFile
file_paths = []
basepath = 'my_directory/'
with os.scandir(basepath) as entries:
for root, directories, files in os.walk(basepath):
for entry in entries:
if entry.is_file():
file_path = os.path.join(entry)
file_paths.append(file_path)
with ZipFile('%s.zip' %(file_path,),'w') as zip:
print("FILE:", entry.name)
for entry in file_paths:
zip.write(entry)
I stumbled upon this unanswered query while I was go though something else, a simple work around would be to use .format as below
with ZipFile('{}.zip'.format(d),'w') as zip:

Python delete file in multiple folders

In multiple folders I have a file called _status.json
e.g.:
C:\Users\Me\.fscrawler\Folder1\_status.json
C:\Users\Me\.fscrawler\Folder2\_status.json
....
C:\Users\Me\.fscrawler\*\_status.json
I want to write a short python code, to delete all those files.
I already tried the following code, but it does not work. I dont know why, but I think the solution is pretty easy
import os
os.remove(C:\Users\Me\.fscrawler\*\_status.json)
You will have to walk through all the subfolders to find and delete the file.
for root, dirs, files in os.walk(folder_path):
for name in files:
if name == '_status.json':
#delete the file
I would look into the glob module, and use it to find the files:
example:
import glob
relative_path_to_files = glob.glob('**/_status.json', recursive=True)
then you can operate on the list as you wish :)
Edit:
relative_path_to_files is a list, so you have to iterate over its elements and operate on them:
here is a complete example to find all _status.json in the current directory and its sub-tree recursively:
import glob
import os
for f in glob.glob('**/_status.json', recursive=True):
os.remove(f)

how to get name of a file in directory using python

There is an mkv file in a folder named "export". What I want to do is to make a python script which fetches the file name from that export folder.
Let's say the folder is at "C:\Users\UserName\Desktop\New_folder\export".
How do I fetch the name?
I tried using this os.path.basename and os.path.splitext .. well.. didn't work out like I expected.
os.path implements some useful functions on pathnames. But it doesn't have access to the contents of the path. For that purpose, you can use os.listdir.
The following command will give you a list of the contents of the given path:
os.listdir("C:\Users\UserName\Desktop\New_folder\export")
Now, if you just want .mkv files you can use fnmatch(This module provides support for Unix shell-style wildcards) module to get your expected file names:
import fnmatch
import os
print([f for f in os.listdir("C:\Users\UserName\Desktop\New_folder\export") if fnmatch.fnmatch(f, '*.mkv')])
Also as #Padraic Cunningham mentioned as a more pythonic way for dealing with file names you can use glob module :
map(path.basename,glob.iglob(pth+"*.mkv"))
You can use glob:
from glob import glob
pth ="C:/Users/UserName/Desktop/New_folder/export/"
print(glob(pth+"*.mkv"))
path+"*.mkv" will match all the files ending with .mkv.
To just get the basenames you can use map or a list comp with iglob:
from glob import iglob
print(list(map(path.basename,iglob(pth+"*.mkv"))))
print([path.basename(f) for f in iglob(pth+"*.mkv")])
iglob returns an iterator so you don't build a list for no reason.
I assume you're basically asking how to list files in a given directory. What you want is:
import os
print os.listdir("""C:\Users\UserName\Desktop\New_folder\export""")
If there's multiple files and you want the one(s) that have a .mkv end you could do:
import os
files = os.listdir("""C:\Users\UserName\Desktop\New_folder\export""")
mkv_files = [_ for _ in files if _[-4:] == ".mkv"]
print mkv_files
If you are searching for recursive folder search, this method will help you to get filename using os.walk, also you can get those file's path and directory using this below code.
import os, fnmatch
for path, dirs, files in os.walk(os.path.abspath(r"C:/Users/UserName/Desktop/New_folder/export/")):
for filename in fnmatch.filter(files, "*.mkv"):
print(filename)
You can use glob
import glob
for file in glob.glob('C:\Users\UserName\Desktop\New_folder\export\*.mkv'):
print(str(file).split('\')[-1])
This will list out all the files having extention .mkv as
file.mkv, file2.mkv and so on.
From os.walk you can read file paths as a list
files = [ file_path for _, _, file_path in os.walk(DIRECTORY_PATH)]
for file_name in files[0]: #note that it has list of lists
print(file_name)

Categories

Resources