pyworkflow.utils.path module

This module contains the PATH related utilities inside the utils module


Create directory “backup” if necessary and back up the file.






Remove a list of paths, either folders or files


Remove all files that match the pattern.


Return the common longest prefix path. It uses the python os.path.commonprefix and then the direname over it since the former is implemented in char-by-char base.

pyworkflow.utils.path.copyFile(source, dest)[source]

Shortcut to shutil.copy.

pyworkflow.utils.path.copyPattern(pattern, destFolder)[source]

Copy all files matching the pattern to the given destination folder.

pyworkflow.utils.path.copyTree(source, dest)[source]

Wrapper around the shutil.copytree, but allowing that the dest folder also os.path.exists.

Creates a link to a given file path

Creates a relative link to a given file path. Try to use common path for source and dest to avoid errors. Different relative paths may exist since there are different valid paths for a file, it depends on the current working dir path


This function creates a file name that is similar to the original by adding a unique numeric suffix. check NamedTemporaryFile from tempfile for alternatives

pyworkflow.utils.path.expandPattern(pattern, vars=True, user=True)[source]

Expand environment vars and user from a given pattern.

pyworkflow.utils.path.findFile(filename, *paths, recursive=False)[source]

Search if a file/folder is present in any of the paths provided.

  • filename – Name (myfile.txt) of the file to look for.

  • paths – N number of folders to look at.

  • recursive – If True it will iterate into the subfolders.


None if nothing is found.

pyworkflow.utils.path.findFileRecursive(filename, path)[source]

Finds a file/folder in a single path recursively

  • filename – Name (myfile.txt) of the file to look for.

  • path – folder to start the search from.


The absolute path to the ‘filename’ found or None if not found.

pyworkflow.utils.path.findRootFrom(referenceFile, searchFile)[source]

This method will find a path (root) from ‘referenceFile’ from which the ‘searchFile’ os.path.exists. A practical example of ‘referenceFile’ is a metadata file and ‘searchFile’ is an image to be found from the metadata. Return None if the path is not found.


Return the extension given a file.


Returns the last modification date of a file or None if it doesn’t exist.


Shortcut to inspect the size of a file or a folder.


Gets all files of given folder and it subfolders. folderPath – Folder path to get files. returns – Set with all folder files.


Return the home path of a give user.


Return the last file matching the pattern.


Returns the absolute parent folder of a file or folder. Work for folders that ens with “/” which dirname can’t

pyworkflow.utils.path.hasChangedSince(fn, time)[source]
Returns if the file has changed since the timestamp passed as parameter. It will check

the last modified time of the file this set uses to persists.


time – timestamp to compare to the last modification time

pyworkflow.utils.path.isFileFinished(fn, duration=60)[source]

Returns True if the file (fn) last modification has not changed for 60 seconds (default duration)

pyworkflow.utils.path.iterBigFile(textfile, offset=0, size=None, maxSize=400, headSize=40, tailSize=None)[source]

Yield lines from file textfile. If the size to read is bigger than maxSize then yield the first lines until headSize bytes, then yield None, then yield the last lines from tailSize bytes to the end.


Join several path parts with a .


Make the path to ensure that files can be written.


Create a list of paths if they don’t os.path.exists. Recursively create all folder needed in a path. If a path passed is a file, only the directory will be created.


Create the scratch folder if SCIPION_SCRATCH variable is defined into the Scipion config, i.o.c create tmp folder


Check if the list of paths os.path.exists. Will return the list of missing files, if the list is empty means that all path os.path.exists

pyworkflow.utils.path.moveFile(source, dest)[source]

Move file from source to dest.

pyworkflow.utils.path.moveTree(src, dest)[source]

Take the basename of the filename and remove extension


Remove extension from basename

pyworkflow.utils.path.renderLine(line, add, lineNo=1, numberLines=True)[source]

Find all the fragments of formatted text in line and call add(fragment, tag) for each of them.

pyworkflow.utils.path.renderTextFile(fname, add, offset=0, lineNo=0, numberLines=True, maxSize=400, headSize=40, tailSize=None, notifyLine=None, errors='strict')[source]

Call callback function add() on each fragment of text from file fname, delimited by lines and/or color codes.

  • add – callback function with signature (txt, tag=’normal’)

  • offset – byte offset - we start reading the file from there

  • lineNo – lines will be numbered from this value on

  • numberLines – whether to prepend the line numbers

pyworkflow.utils.path.replaceBaseExt(filename, newExt)[source]

Replace the current basename extension(from last .) with a new one. The new one should not contains the .

pyworkflow.utils.path.replaceExt(filename, newExt)[source]

Replace the current path extension(from last .) with a new one. The new one should not contain the .