diff --git a/getting_started/03_file_management.ipynb b/getting_started/03_file_management.ipynb index a677f0d0867b13e45ce59782acd44b90dc6d7447..b7a7cbd7ad4bc2f4d5ab151a4b21d4a183c86a6f 100644 --- a/getting_started/03_file_management.ipynb +++ b/getting_started/03_file_management.ipynb @@ -42,23 +42,24 @@ "\n", "\n", "* [Managing files and directories](#managing-files-and-directories)\n", - " * [Querying and changing the current directory](#querying-and-changing-the-current-directory)\n", - " * [Directory listings](#directory-listings)\n", - " * [Creating and removing directories](#creating-and-removing-directories)\n", - " * [Moving and removing files](#moving-and-removing-files)\n", - " * [Walking a directory tree](#walking-a-directory-tree)\n", - " * [Copying, moving, and removing directory trees](#copying-moving-and-removing-directory-trees)\n", + " * [Querying and changing the current directory](#querying-and-changing-the-current-directory)\n", + " * [Directory listings](#directory-listings)\n", + " * [Creating and removing directories](#creating-and-removing-directories)\n", + " * [Moving and removing files](#moving-and-removing-files)\n", + " * [Walking a directory tree](#walking-a-directory-tree)\n", + " * [Copying, moving, and removing directory trees](#copying-moving-and-removing-directory-trees)\n", "* [Managing file paths](#managing-file-paths)\n", - " * [File and directory tests](#file-and-directory-tests)\n", - " * [Deconstructing paths](#deconstructing-paths)\n", - " * [Absolute and relative paths](#absolute-and-relative-paths)\n", - " * [Wildcard matching a.k.a. globbing](#wildcard-matching-aka-globbing)\n", - " * [Expanding the home directory and environment variables](#expanding-the-home-directory-and-environment-variables)\n", - " * [Cross-platform compatibility](#cross-platform-compatbility)\n", + " * [File and directory tests](#file-and-directory-tests)\n", + " * [Deconstructing paths](#deconstructing-paths)\n", + " * [Absolute and relative paths](#absolute-and-relative-paths)\n", + " * [Wildcard matching a.k.a. globbing](#wildcard-matching-aka-globbing)\n", + " * [Expanding the home directory and environment variables](#expanding-the-home-directory-and-environment-variables)\n", + " * [Cross-platform compatibility](#cross-platform-compatbility)\n", + "* [FileTrees](#filetree)\n", "* [Exercises](#exercises)\n", - " * [Re-name subject directories](#re-name-subject-directories)\n", - " * [Re-organise a data set](#re-organise-a-data-set)\n", - " * [Solutions](#solutions)\n", + " * [Re-name subject directories](#re-name-subject-directories)\n", + " * [Re-organise a data set](#re-organise-a-data-set)\n", + " * [Solutions](#solutions)\n", "* [Appendix: Exceptions](#appendix-exceptions)\n", "\n", "\n", @@ -79,7 +80,8 @@ "outputs": [], "source": [ "import os\n", - "import os.path as op" + "import os.path as op\n", + "from pathlib import Path" ] }, { @@ -111,13 +113,13 @@ "outputs": [], "source": [ "cwd = os.getcwd()\n", - "print('Current directory: {}'.format(cwd))\n", + "print(f'Current directory: {cwd}')\n", "\n", "os.chdir(op.expanduser('~'))\n", - "print('Changed to: {}'.format(os.getcwd()))\n", + "print(f'Changed to: {os.get_cwd()}')\n", "\n", "os.chdir(cwd)\n", - "print('Changed back to: {}'.format(cwd))" + "print(f'Changed back to: {cwd}')" ] }, { @@ -158,13 +160,13 @@ "source": [ "cwd = os.getcwd()\n", "listing = os.listdir(cwd)\n", - "print('Directory listing: {}'.format(cwd))\n", + "print(f'Directory listing: {cwd}')\n", "print('\\n'.join(listing))\n", "print()\n", "\n", "datadir = 'raw_mri_data'\n", "listing = os.listdir(datadir)\n", - "print('Directory listing: {}'.format(datadir))\n", + "print(f'Directory listing: {datadir}')\n", "print('\\n'.join(listing))" ] }, @@ -961,6 +963,40 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "> The `Path` object in the built-in [`pathlib`](https://docs.python.org/3/library/pathlib.html) also has\n", + "> excellent cross-platform support. If you write your file management code using this class you are far less likely\n", + "> to get errors on Windows.\n", + "\n", + "<a class=\"anchor\" id=\"filetree\"></a>\n", + "## FileTree\n", + "`fslpy` also contains support for `FileTree` objects (docs are \n", + "[here](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.utils.filetree.html)). \n", + "These `FileTree` objects provide a simple format to define a whole directory structure with multiple subjects, sessions,\n", + "scans, etc. In the `FileTree` format the dataset we have been looking at so far would be described by:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "tree_text = \"\"\"\n", + "raw_mri_data\n", + " subj_{subject}\n", + " rest.nii.gz\n", + " t1.nii\n", + " t2.nii\n", + " task.nii.gz\n", + "\"\"\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "FileTrees are discussed in more detail in the advanced `fslpy` practical.\n", + "\n", "<a class=\"anchor\" id=\"exercises\"></a>\n", "## Exercises\n", "\n", diff --git a/getting_started/03_file_management.md b/getting_started/03_file_management.md index 0c4979a8a3321811f0381fdfb5277b63c75d999d..35ead5cbc6757a380ea8cf902fab94f7f35a4a9b 100644 --- a/getting_started/03_file_management.md +++ b/getting_started/03_file_management.md @@ -36,23 +36,24 @@ other sections as a reference. You might miss out on some neat tricks though. * [Managing files and directories](#managing-files-and-directories) - * [Querying and changing the current directory](#querying-and-changing-the-current-directory) - * [Directory listings](#directory-listings) - * [Creating and removing directories](#creating-and-removing-directories) - * [Moving and removing files](#moving-and-removing-files) - * [Walking a directory tree](#walking-a-directory-tree) - * [Copying, moving, and removing directory trees](#copying-moving-and-removing-directory-trees) + * [Querying and changing the current directory](#querying-and-changing-the-current-directory) + * [Directory listings](#directory-listings) + * [Creating and removing directories](#creating-and-removing-directories) + * [Moving and removing files](#moving-and-removing-files) + * [Walking a directory tree](#walking-a-directory-tree) + * [Copying, moving, and removing directory trees](#copying-moving-and-removing-directory-trees) * [Managing file paths](#managing-file-paths) - * [File and directory tests](#file-and-directory-tests) - * [Deconstructing paths](#deconstructing-paths) - * [Absolute and relative paths](#absolute-and-relative-paths) - * [Wildcard matching a.k.a. globbing](#wildcard-matching-aka-globbing) - * [Expanding the home directory and environment variables](#expanding-the-home-directory-and-environment-variables) - * [Cross-platform compatibility](#cross-platform-compatbility) + * [File and directory tests](#file-and-directory-tests) + * [Deconstructing paths](#deconstructing-paths) + * [Absolute and relative paths](#absolute-and-relative-paths) + * [Wildcard matching a.k.a. globbing](#wildcard-matching-aka-globbing) + * [Expanding the home directory and environment variables](#expanding-the-home-directory-and-environment-variables) + * [Cross-platform compatibility](#cross-platform-compatbility) +* [FileTrees](#filetree) * [Exercises](#exercises) - * [Re-name subject directories](#re-name-subject-directories) - * [Re-organise a data set](#re-organise-a-data-set) - * [Solutions](#solutions) + * [Re-name subject directories](#re-name-subject-directories) + * [Re-organise a data set](#re-organise-a-data-set) + * [Solutions](#solutions) * [Appendix: Exceptions](#appendix-exceptions) @@ -69,6 +70,7 @@ creating, removing, and traversing directories. ``` import os import os.path as op +from pathlib import Path ``` @@ -92,13 +94,13 @@ You can query and change the current directory with the `os.getcwd` and ``` cwd = os.getcwd() -print('Current directory: {}'.format(cwd)) +print(f'Current directory: {cwd}') os.chdir(op.expanduser('~')) -print('Changed to: {}'.format(os.getcwd())) +print(f'Changed to: {os.get_cwd()}') os.chdir(cwd) -print('Changed back to: {}'.format(cwd)) +print(f'Changed back to: {cwd}') ``` @@ -123,13 +125,13 @@ command): ``` cwd = os.getcwd() listing = os.listdir(cwd) -print('Directory listing: {}'.format(cwd)) +print(f'Directory listing: {cwd}') print('\n'.join(listing)) print() datadir = 'raw_mri_data' listing = os.listdir(datadir) -print('Directory listing: {}'.format(datadir)) +print(f'Directory listing: {datadir}') print('\n'.join(listing)) ``` @@ -730,6 +732,27 @@ print(path) print(op.join(op.sep, 'home', 'fsluser', '.bash_profile')) ``` +> The `Path` object in the built-in [`pathlib`](https://docs.python.org/3/library/pathlib.html) also has +> excellent cross-platform support. If you write your file management code using this class you are far less likely +> to get errors on Windows. + +<a class="anchor" id="filetree"></a> +## FileTree +`fslpy` also contains support for `FileTree` objects (docs are +[here](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.utils.filetree.html)). +These `FileTree` objects provide a simple format to define a whole directory structure with multiple subjects, sessions, +scans, etc. In the `FileTree` format the dataset we have been looking at so far would be described by: +``` +tree_text = """ +raw_mri_data + subj_{subject} + rest.nii.gz + t1.nii + t2.nii + task.nii.gz +""" +``` +FileTrees are discussed in more detail in the advanced `fslpy` practical. <a class="anchor" id="exercises"></a> ## Exercises