os.fs

Low-level file manipulation.

All function here should be platform independent, should not involve globbing or logging (unless in case of unexpected failure).

Attributes

CYGPATH_MATCH

logger

Classes

OSFSError

Functions

cd(→ None)

Change current directory.

chmod(→ int)

Chmod with interface similar to Unix tool.

df(…)

Disk space available on the filesystem containing the given path.

__safe_unlink_func(...)

Provide a safe unlink function on windows.

force_remove_file(→ None)

Force file removing, changing permissions if first attempt failed.

ldd_output_to_posix(→ str)

Transform an ldd output to POSIX paths only.

max_path(→ int)

Return the maximum length for a path.

mv(→ None)

Move a file.

readlink(→ str)

Get target path of a symlink.

touch(→ None)

Update file access and modification times. Create the file if needed.

unixpath(→ str)

Convert path to Unix/Cygwin format.

which(→ Any)

Locate executable.

Module Contents

os.fs.CYGPATH_MATCH: re.Pattern[str]
class os.fs.OSFSError

Bases: e3.error.E3Error

os.fs.logger
os.fs.cd(path: str | pathlib.Path) None

Change current directory.

Parameters:

path – directory name

Raises:

OSFSError – in case of error

os.fs.chmod(mode: str, filename: str | pathlib.Path) int

Chmod with interface similar to Unix tool.

Parameters:
  • mode – should conform with posix specification for chmod utility (ex: +wx). See chmod man page for more information

  • filename – the target file

Returns:

the mode that has been set

os.fs.df(path: str | pathlib.Path) int
os.fs.df(path: str | pathlib.Path, full: Literal[True]) tuple

Disk space available on the filesystem containing the given path.

Parameters:
  • path – a path

  • full – if True return full disk information otherwise only space left.

Returns:

either space left in Mo or a py:meth:collections.namedtuple with total, used and free attributes. Each attribute is an int representing Mo.

Provide a safe unlink function on windows.

Note that all this is done to ensure that rm is working fine on Windows 7 and 2008R2. Indeed very often, deletion will fail with access denied error. The typical scenario is when you spawn an executable and try to delete it just afterward.

os.fs.force_remove_file(path: str | pathlib.Path) None

Force file removing, changing permissions if first attempt failed.

Parameters:

path – path of the file to remove

os.fs.ldd_output_to_posix(ldd_output: str) str

Transform an ldd output to POSIX paths only.

This method does not have any impact when the ldd output has been executed on a Unix host, because paths are already POSIX there.

It applies only to ldd outputs on Windows, where the paths starting with /c/, /mnt/c/ or /cygdrive/c/ are modified to C:/.

For instance, /c/WINDOWS/System32/ntdll.dll is transformed to C:/WINDOWS/System32/ntdll.dll.

Note

The transformation is made on strings only to minimize the call the cygpath. If a file path is found, all its occurrences are replaced by its POSIX value.

Parameters:

ldd_output – The output of an ldd call to transform to contain only POSIX paths.

Returns:

An ldd output with POSIX paths only.

os.fs.max_path() int

Return the maximum length for a path.

Returns:

the maximum length

os.fs.mv(source: str | pathlib.Path, target: str | pathlib.Path) None

Move a file.

Parameters:
  • target – file to move

  • source – target file or directory

Get target path of a symlink.

Equivalent of os.readlink with support for WSL Windows links.

Parameters:

filename – path containing a symlink

Returns:

target of the symlink

os.fs.touch(filename: str | pathlib.Path) None

Update file access and modification times. Create the file if needed.

Parameters:

filename – file to update

os.fs.unixpath(path: str | pathlib.Path) str

Convert path to Unix/Cygwin format.

Parameters:

path – path string to convert

Returns:

the converted path

On Unix systems this function is identity. On Win32 systems it removes drive letter information and replace \ by /.

os.fs.which(prog: str | pathlib.Path, paths: str | None = None, default: Any = '') Any

Locate executable.

Parameters:
  • prog – program to find

  • paths – if not None then we use this value instead of PATH to look for the executable.

  • default – default value to return if not found

Returns:

absolute path to the program on success, found by searching for an executable in the directories listed in the environment variable PATH or default value if not found