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)
Related
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
I have a folder structure in the following way:
I need to go to the folder where csv files are present and add header to them and then go to the next folder , add header and so on. How to iterate through the multiple folders and do the changes to the csv file using python? Can anyone please tell me how to do it in 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.
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.
i started learning python a few months ago, and I'm trying to solve a challenge that requires me to go through many [2,000~] zip files in a folder, collect all the comments in them and find a clue.
The part that I'm struggling with is the extraction of the comments.
I imported the zipfile module, but I'm not sure how to make it go through all the files in the folder that contain the zip files, and collect all the comments.
I'm using pycharm, and I would't mind if the result will be in the preview area insde pycharm or exported to a new .txt file
can anyone help me?
For looping over files, I tend to use the glob module in python. It returns a list of files that match the string you specify (see docs). Then once you have the list of files, you can loop over them and run some function/code on each one in turn.
import glob
list_of_files = glob.glob("/path/to/directory/*.zip")
for f in list_of_files:
***insert code for each zip file****
for i in os.listdir(path_to_folder):
if i.endswith('.zip'):
<< Your code here>>
Try this and let me know if any issues
The comments in individual files can be accessed using getinfo function in zipfile module, i.e., getinfo(file_name).comment() as explained in this post.