Exclude directories using python dirsync module - python-3.x

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']

Related

How to rename multiple files in Linux?

I want to rename my files in linux from
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-20161127175734_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-20161127175734_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-20161127180331_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-20161127180331_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-20161127173234_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-20161127173234_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-20161127173809_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-20161127173809_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-rest_run-20161127164822_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-rest_run-20161127164822_bold.nii.gz
to be
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-1_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-1_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-2_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-2_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-1_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-1_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-2_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-2_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-rest_run-1_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-rest_run-1_bold.nii.gz
Can anyone help?
I have a list of similar files in different directories too. So, I want it to be a generic code that I can use, if possible.

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".

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

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.

How can I set an universal working directory?

I'm setting up a new algorithm and I want to share it on another computer. How can I set paths with only the folder name, for example.
I've tried to use the function os.chdir(path), but I don't achieve to reach my folder when I'm not using a 'root path' like C:/../../folder.
os.chdir(../folder_name)
By doing os.chdir(../folder_name), you are referring to the parent dir of the current directory.
You probably are looking for the os.chdir(./folder_name), with one dot? This is the current directory, where your script stands.
More here: https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats
Update: After clarification in the comments, you want to refer to folder_name from one of its subfolder (/folder_name/library). Then it is indeed .. that you should use, but .. only:
os.chdir(..)

Python cx_freeze Create dirs for included files in build

Is it possible to create dirs(folders) on cx_freeze build output, cause i include(include_files) many databases files and i want these to be in specific folder etc. I can take them easily from my folders.....
"include_files": ["databases/nations.txt","databases/newafrica.txt",
"databases/neweeurope.txt","databases/neweurope.txt","databases/newmeast.txt","graph.py",
"databases/newnamerica.txt","databases/plates.txt",
"databases/ACN/rigidA.txt","databases/ACN/rigidB.txt",
"databases/ACN/rigidC.txt","databases/ACN/rigidD.txt","databases/ACN/flexibleA.txt",
"databases/ACN/flexibleB.txt","databases/ACN/flexibleC.txt",
"databases/ACN/flexibleD.txt","alternates.xlsx",
but this will just copy all of them in exe build dir and its a mess.
Thanks in advance.
There are several ways you can go around solving the problem.
Method 1 - Using include_files
Rather than ask for each individual text file you could just put the file name in the setup script and leave out the individual text files. In your case it would be like this:
"include_files": ["databases"]
This would copy the entire databases folder with everything in it into you build folder.
Absolute file paths work as well.
If you are going to use the installer feature (bdist_msi) this is the method to use.
You can copy sub folders only using "include_files": ["databases/ACN"]
Method 2 - Manually
Ok it's rather un-pythonic but the one way to do it is to copy it manually into the build folder.
Method 3 - Using the os module
Much the same as method two it would copy the folder into your build folder but instead of coping it manually it would use Python. You also have the option of using additional Python features as well.
Hope I was helpful.

Resources