Can Pathlib iterdir be used to recursively iterate through a directory structure like os.walk? - python-3.x

I know that glob exists for this purpose and one can also use os.walk to get a list of all subdirectories and files inside a folder and use this to create a tree of all files and folders that exist inside a root directory. However, can one also use the Pathlib iterdir function to recursively iterate through all subdirectories inside a folder and store the paths to all files and the filenames like we can do so easily with os.walk?
I am asking this question since as per my knowledge, the Pathlib actually presents better way to do things already done by os and os.path.

Related

Node readdirSync and getting paths dynamically

I have a directory whose contents shrink and grow. I am trying to run a program that will list all the directories.
Is there a way for fs.readdirSync to be fed a path like ./src/images/*/*/* and it would list all the paths within that wildcard structure?
The only alternative I can see is creating some complex nested loops.

Exclude directories using python dirsync module

I want to sync two different directories using the dirsync module, but exclude some specific folders.
In the documentation (https://pypi.org/project/dirsync/) it says the exclude need to be a regex pattern but I cant quite make it work.
For example, lets say we have these directories
c:\folder1\folder2
c:\folder1\folder3
d:\folder1\
I want to sync c:\folder1\ with d:\folder1\ and exclude folder3, so basically the folder c:\folder1\folder2 will be copied and created in d:\
from dirsync import sync
src = r'c:\folder1'
dst = r'd:\folder1'
sync(src, dst, 'diff', exclude='^folder3')
this won't work and I can't quite understand why.
The exclude option expects a list of regexp:
exclude=['^folder3']

Python - working with unkown directory name versions

I'm using python to download an application from a content distribution network. The application downloads as self extract file cabinet. When executed it creates a directory using a version naming format. For example app-version2/../app.exe, Thus I cannot rely on the folder name as it may change in the future. I'm trying to find the best way to work with the content inside the folder without depending on the actual folder name.
My idea was to rename the folder using os.listdir() and then os.rename(app-version2, myapp) This would work but is not automated. What would be the best automated method to find a folder name that contains version numbers and change that to something more static?
Assuming you want to find the path of the directory which begins with app, you can accomplish this using pathlib.Path:
from pathlib import Path
app_path = next(Path().glob('app*'))
This will give you the path to the first file or directory in your current directory whose name begins with "app".

How to get the paths of files in sub directories that contain specific string in a directory python

I have a directory that contains few sub directories. In each sub directory there are few files, and I'd like to get the full paths of the files that contain the word "raw" in their filename and that ends with ".txt" (in each directory there is a file with "raw" in it's name).
Anyone knows how to do it elegantly with python 3?
We can use the Path.resolve method to get the absolute path to the directory, then use Path.glob to collect the Path objects representing the paths of the files we want to retrieve.
from pathlib import Path
paths = [path for path in Path('path/to/dir').resolve().glob("**/*raw*.txt")]
Here your search is specified by **/*raw*.txt. This will return some number of Path objects (the exact type will depend on the operating system you use).
If you want them as strings, you can substitute [str(path) for path in ..] above

MATLAB, Octave: working with folder names that have space in them

In MATLAB, actually Octave, I would like to find a list of all subfolders in the current folder so I use this:
subFolder = dir;
This gives the list of all subfolders in the current folder. This returns a structure whose one element is the name. Assume I have two subfolders with names subfolder 1A and subfolder 1B.
Now I want to go to these folders. Then I do this:
cd subFolder(1).name
But I get this error:
error: subFolder(1).name: No such file or directory
If I do this:
cd "subfolder 1A"
everything works fine. What is the solution?
The space in the folder name is a red herring in this case. It's not the source of the problem. The actual issue is that you need to call the cd function using function syntax instead of command syntax (i.e. use parentheses; related question here):
cd(subFolder(1).name);
When you use the command syntax, subFolder(1).name is itself being treated as the string argument to cd (i.e. it's looking for a folder called 'subFolder(1).name'). With the function syntax, the string contained within the structure array field is used as the argument.
To make your code a little more robust, you could also use the 'folder' field returned by dir:
cd(fullfile(subFolder(1).folder, subFolder(1).name));
This will go to the desired folder regardless of the directory you are currently in, since it specifies an absolute path instead of a partial path (which is relative to the current directory).

Resources