how to read multiple wav files in python? - python

I want to read multiple wav files in a loop rather than reading every single one at one time
for, using os library, as I try to read files with this code I get error of file not found
The files are in different sub folder inside the main folder refer (GTZAN Dataset)
eg:
r = wav.read(directory+folder+r"\\"+file)
Since I've used OS library for pathing in this project, would like the answer in OS pathing format rather than regular pathing .
Note: I've declared directory in my code, it contains the original path in which a specific folder contains multiple wav files
I am able to get the folders and list them and also list the wav files in the subfolder but at the end not able to read them
The above code is the only I've tried since am using OS library pathing format.
I am able to get the folders and list them and also list the wav files in the subfolder but at the end not able to read them

Related

Is there an easy way to handle inconsistent file paths in blob storage?

I have a service that drops a bunch of .gz files to an azure container on a daily cadence. I'm looking to pick these files up and convert the underlying txt/json into tables. The issue perplexing me is that the service adds two random string prefix folders and a date folder to the path.
Here's an example file path:
container/service-exports/z633dbc1-3934-4cc3-ad29-e82c6e74f070/2022-07-12/42625mc4-47r6-4bgc-ac72-11092822dd81-9657628860/*.gz
I've thought of 3 possible solutions:
I don't necessarily need the data to persist. I could theoretically loop through each folder and look for .gz, open and write them to an output file and then go back through and delete the folders in the path.
Create some sort of checkpoint file that keeps track of each path per gzip and then configure some way of comparison to the checkpoint file at runtime. Not sure how efficient this would be over time.
Use RegEx to look for random strings matching the pattern/length of the prefixes and then look for the current date folder. If the date isn't today, pass.
Am I missing a prebuilt library or function capable of simplifying this? I searched around but couldn't find any discussions on this type of problem.
You can do this using koalas.
import databricks.koalas as ks
path = "wasbs://container/service-exports/*/*/*.gz"
df = ks.read_csv(path, sep="','", header='infer')
This should work out well if all the .gz files have the same columns, then df would contain all the data from .gz files concatenated.

Get latest files in each sub folders using python

I have a list of folders I want to process and I want to get the latest files in each sub-directories.
For example, I want to find files that contain Received in the files name from each sub-directory. However, in each sub-directories, there might be multiple files with Received in their name. So I want to get the latest one only, meaning, only one file from each sub-folder.
Root_path = R:\\test
How can I achieve this please use python.

Remove folders but the csv files

I have a folder (folder_1) that consists of many different subfolders, these subfolders (subfolder_1, subfolder_2, etc etc) all consist of 1 csv file. I'd like to delete all subfolders, and just keep all csv files. Is there a way to achieve this without specifying all subfolders?
Maybe a way to make an exception for csv files using shutil?
Thanks in advance.
What you can do is:
Loop over the subfolders.
Check this post.
Get the csv file(s) path. (replace the .txt to .csv from previous post)
Use the shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo") to move the file on a higher level.
use shutil.rmtree() to remove folder. (check here too)

how to extract a continuously updable Zip file on a website by Selenium and then unzip it for specific file names

I have used Selenium x Python to download a zip file daily but i am currently facing a few issues after downloading it on my local download folder
is it possible to use Python to read those files dynamically? let's say the date is always different. Can we simply add wildcard*? I am trying to move it from downloader folder to another folder but it always require me to name the file entirely.
how to unzip a file and look for specific files there? let's say those file will always start with files names "ABC202103xx.csv"
much appreciate for your help! any sample code will be truly appreciate!
Not knowing the excact name of a file in a local folder should usually not be a problem. You could just list all filenames in the local folder and then use a for loop to find the filename you need. For example let's assume that you have downloaded a zip file into a Downloads folder and you know it is named "file-X.zip" with X being any date.
import os
for filename in os.listdir("Downloads"):
if filename.startswith("file-") and filename.endswith(".zip"):
filename_you_are_looking_for = filename
break
To unzip files, I will refer you to this stackoverflow thread. Again, to look for specific files in there, you can use os.listdir.

Want to compile all the images in a folder and its subfolders to pdf file with names

I am just a beginner in Python. So help me learn and write the code for a small but complex problem. I've tried many things but I am lost where to start with and go:
Problem:
I have a folder and its subfolders with heaps of different product images(let's say in .jpeg and .png). I want to compile a single pdf of all these photos with links/location of these photos in the pdf. This list and photos could be in the form of a table or a very simple format.
I am doing it because sometimes I forget the product name so I have to look at its image by going into each folder and sub-folder. This will give me an opportunity to look at all the photos in these folders and sub-folder without opening them one-by-one.
Your issue breaks down into 3 steps.
1-Search the directories for files (which you can use the os module's walk()).
here is a great tutorial:
https://www.pythoncentral.io/how-to-traverse-a-directory-tree-in-python-guide-to-os-walk/
2-add the found files into a list of tuples having path of the image and the name of it.
3- Add these images into a single pdf file. You can use python module fpdf to do this. And this has been addressed already here:
Create PDF from a list of images

Categories

Resources