Extract a part of a directory in Python 3 - python

I know that similar questions were previously asked but I couldn't find what I need.
For example I have a directory /home/user/dir that contains 5 folders named from 1 to 5.
I need to create a list that will have the following form ['dir/1', 'dir/2','dir/3','dir/4','dir/5'].
I am using Python 3.5 and Ubuntu 14.04.
Thank you for your help!

here is something that can help you.
import glob
dirs = glob.iglob('whatever/the/path/*')
dir_list = ['dir/'+dir for dir in dirs]
In case you have a './' shown before every of you dir, you can easily do
dir_list = ['dir/'+dir[2:] for dir in dirs]
to remove them.

Related

list of name and path of files in a directory and subdirectory where one or more subdir have a point in the middle python

I'm trying to get path and filename, from a directory, including those ones inside the subdirectories. The problem is that some subfolders have one or more points in the name.
So when I execute this code
listaFile=glob.glob('c:\test\ID_1'+/**/*.*',recursive= True)
I get
c:\test\ID_1\fil.e1.txt
c:\test\ID_1\fil.e2.doc
c:\test\ID_1\subfolder1\file1.txt
c:\test\ID_1\sub.folder2 (instead of c:\test\ID_1\sub.folder2\file1.txt)
thank you all in advance!
You need to filter it out checking if it's a file or folder. An easy way would be to use the pathlib instead of glob directly. Example below.
listaFile = [str(path) for path in pathlib.Path(r"c:\test\ID_1").rglob("*.*") if path.is_file()]

Python get all the file name in a list

The problem is to get all the file names in a list that are under a particular directory and in a particular condition.
We have a directory named "test_dir".
There, we have sub directory "sub_dir_1", "sub_dir_2", "sub_dir_3"
and inside of each sub dir, we have some files.
sub_dir_1 has files ['test.txt', 'test.wav']
sub_dir_2 has files ['test_2.txt', 'test.wav']
sub_dir_2 has files ['test_3.txt', 'test_3.tsv']
What I want to get at the end of the day is a list of of the "test.wav" that exist under the "directory" ['sub_dir_1/test.wav', 'sub_dir_2/test.wav']. As you can see the condition is to get every path of 'test.wav' under the mother directory.
mother_dir_name = "directory"
get_test_wav(mother_dir_name)
returns --> ['sub_dir_1/test.wav', 'sub_dir_2/test.wav']
EDITED
I have changed the direction of the problem.
We first have this list of file names
["sub_dir_1/test.wav","sub_dir_2/test.wav","abc.csv","abc.json","sub_dir_3/test.json"]
from this list I would like to get a list that does not contain any path that contains "test.wav" like below
["abc.csv","abc.json","sub_dir_3/test.json"]
You can use glob patterns for this. Using pathlib,
from pathlib import Path
mother_dir = Path("directory")
list(mother_dir.glob("sub_dir_*/*.wav"))
Notice that I was fairly specific about which subdirectories to check - anything starting with "sub_dir_". You can change that pattern as needed to fit your environment.
Use os.walk():
import os
def get_test_wav(folder):
found = []
for root, folders, files in os.walk(folder):
for file in files:
if file == "test.wav":
found.append(os.path.join(root, file))
return found
Or a list comprehension approach:
import os
def get_test_wav(folder):
found = [f"{arr[0]}\\test.wav" for arr in os.walk(folder) if "test.wav" in arr[2]]
return found
I think this might help you How can I search sub-folders using glob.glob module?
The main way to make a list of files in a folder (to make it callable later) is:
file_path = os.path.join(motherdirectopry, 'subdirectory')
list_files = glob.glob(file_path + "/*.wav")
just check that link to see how you can join all sub-directories in a folder.
This will also give you all the file in sub directories that only has .wav at the end:
os.chdir(motherdirectory)
glob.glob('**/*.wav', recursive=True)

Renaming multiple sub folders to match the parent folder's name

I'm a python beginner but have some basic experience, and I need someone to please help me use the os module to rename sub folders based on their parent folder. I've been searching for answers for the past week and have not had any success. I'm assuming I need to use the os.walk method to do this.
Here is my folder structure:
C:\data\test\
C:\data\test\map1
C:\data\test\map1\1617151
C:\data\test\map2
C:\data\test\map2\181719
C:\data\test\map3
C:\data\test\map3\182726
C:\data\test\map4
C:\data\test\map4\894932
I need the results to look like this.
C:\data\test\
C:\data\test\map1
C:\data\test\map1\map1
C:\data\test\map2
C:\data\test\map2\map2
C:\data\test\map3
C:\data\test\map3\map3
C:\data\test\map4
C:\data\test\map4\map4
Can someone please help?
python 2.7:
import os
os.chdir("C:\data\test\") # go to dir
sub_dirs = os.walk('.').next()[1] # get list of subdirs
for sub_dir in sub_dirs:
sub_sub_dir = os.walk('.').next[1] # get sub-subdir
os.rmdir(sub_sub_dir) # remove sub-subdir
os.makedirs(sub_dir + '\bla') # make new sub-subdir named subdir\bla
python 3+:
import os
os.chdir("C:\data\test\")
sub_dirs=next(os.walk('.'))[1]
for sub_dir in sub_dirs:
sub_sub_dir = next(os.walk('.'))[1]
os.rmdir(sub_sub_dir)
os.makedirs(sub_dir + '\bla')
Untested, but should do it.
You can get a list of all the files and it's respective folder location using this one liner:
here = '.' # Current location
files = [(root, files) for root, dirs, files in os.walk(here) if (not dirs and files)]
For the given folder structure it will return:
[
('C:\data\test\map1', ['1617151']),
...
]
You can now loop over this list and rename the files (https://docs.python.org/3/library/os.html#os.rename). You can get the parent folder's name by splitting the root string (root.split('\')[-1]).

Get subset of path with specified folder

Suppose i have a path:
'C:\\Folder1\\Folder2\\Folder3\\Folder4'
Question is How Can i get subset of this path up to specified folder plus one directory down from specified folder.
Of course this should be generic, so folder names could be different.
For example with path from above, I specify such directory:
'Folder2'
And i want to get this path as a result:
'C:\\Folder1\\Folder2\\Folder3'
the os library has lots of features to manage paths. then a recursive method could allow to find the correct folder. try something like this:
import os
def find_folder( path, folder_name):
head, tail = os.path.split(path)
if folder_name == os.path.split(head)[1]:
return path
else:
return find_folder(head, folder_name)
path = 'C:\\Folder1\\Folder2\\Folder3\\Folder4'
print find_folder(path, 'Folder2')
hi you can try to split the path for example
import os
a=r"'C:\\Folder1\\Folder2\\Folder3\\Folder4'"
a.split(os.pathsep)
result is :
['C:', 'Folder1', 'Folder2', 'Folder3', 'Folder4']
remove the one you want
and concatenate after that the path .
Thanks and good luck !

Python Kivy: Search directory for filetype

There are many ways, to search a dir for containing a string, that's not really my question. But is there something built in for Python Kivy, that allows automatically searching for files (*.mp3) in a directory with subdirs, or do I have to create one on my own?
If I have to do so, how do I get all the files and subdirs in a directory?
Thank you :)
Finally I decided coding the needed function on my own:
import os
def listfiles(path):
files = []
for base, directory, filename in os.walk(path):
for i in range(len(filename)):
files.append(base+"/"+filename[i])
return files
print(listfiles("/path/path/"))
Checking files for extensions should be easy enaugh :)
Unfortunately the process might take very long for bigger directories, so I'm still looking for a different solution.
Check out Kivy kivy.uix.filechooser FileChooserController and its method files
The list of files in the directory specified by path after applying the filters.
files is a read-only ListProperty.
#edit
Here's what I also found in Kivy docs and this one seems even nicer:
from kivy.uix.filechooser import FileSystemLocal
file_system = FileSystemLocal()
file_system.listdir('/path/to/dir') # this returns a list of files in dir
Instead of FileSystemLocal you can also use FileSystemAbstract if you are not going to browse only local files.
files = [] # list with files in directory
suff = ('.mp3', '.wav')
for i in files:
if i.endswith(suff):
print files
Suffixes needs to be tuples for this to work.

Categories