'delete all files from a path and keep only .parquet files using python
I have a path mydir where i have file1,file2, .. file100 each folder have .crc,.bak etc, files i want to remove all files and keep only .parquet files and name the .parquet files with folder name for eg., file1 folder have .crc,.bak files after removing we end up with .parquet i need to name this as file1.parquet.
I tried to remove one folder but couldnot do it for all folders uisng python
can someone help me how to solve this
mydir='c/users/name/files'
for f in os.listdir(mydir):
if f.endswith(".parquet"):
continue
os.remove(os.path.join(mydir, f))
Solution 1:[1]
Following Miguel's comment, you can use glob like this:
import glob
import os
mydir = "./"
dir_to_remove = []
for src_file in glob.glob(mydir + '**/.parquet', recursive=True):
dir_, file_ = src_file.rsplit('/', 1)
dir_to_remove.append(dir_)
dst_file = dir_ + file_
os.rename(src_file, dst_file)
for dir_ in dir_to_remove:
os.rmdir(dir_)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
