Programming: Useful file tools in Python - spinalcordtoolbox/spinalcordtoolbox GitHub Wiki
Most file commands can be found here and here
Most common file commands
cp filename path
-->shutil.copy(filename, path)
rm -rf path
-->shutil.rmtree(path, ignore_errors=True)
mv filepath1 filepath2
-->shutil.move(filepath1, filepath2)
- Note: Do not use
os.rename
! Instead, useshutil.move
for this too, becauseos.rename
has an issue that causes problems withtmp_path
on CI, as well as any users who have files spread across different filesystems.
- Note: Do not use
status, path_sct = commands.getstatusoutput('echo $SCT_DIR')
-->path_sct = os.environ.get('SCT_DIR')
os.path
functions and pathlib.Path
methods
Differences between - Note that
os.path.abspath()
corresponds topathlib.Path.resolve()
, notpathlib.Path.absolute()
! This is because of the different handling of..
inside paths.