other:python:misc_by_jyp
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | Next revisionBoth sides next revision | ||
other:python:misc_by_jyp [2023/09/27 15:53] – Added a section with a link to "100 numpy exercises" jypeter | other:python:misc_by_jyp [2023/11/29 11:32] – [Working with paths and filenames] Improved and added pathlib example jypeter | ||
---|---|---|---|
Line 65: | Line 65: | ||
==== Working with paths and filenames ==== | ==== Working with paths and filenames ==== | ||
- | If you are in a hurry, you can just use string functions to work with path and file names. | + | If you are in a hurry, you can just use string functions to work with paths and file names. |
- | | + | |
+ | You will need some specific objects and functions to check if a file exists, and similar operations. Check the libraries listed below, that can automatically deal with Unix-type paths on Linux and MacOS computers, and Windows-type paths on Windows computers | ||
+ | |||
+ | | ||
* Available since... a long time! Use this if you want to avoid backward compatibility problems | * Available since... a long time! Use this if you want to avoid backward compatibility problems | ||
* Some functions are directly in [[https:// | * Some functions are directly in [[https:// | ||
- | * [[https:// | + | * [[https:// |
* Available since Python version 3.4 | * Available since Python version 3.4 | ||
* [[https:// | * [[https:// | ||
- | * [[https:// | + | * [[https:// |
- | === Example: getting the full path of the Python used === | + | === Example: getting the full path of the Python |
Note: the actual python may be different from the default python! | Note: the actual python may be different from the default python! | ||
Line 83: | Line 86: | ||
/ | / | ||
- | $ /modfs/modtools/miniconda3//envs/analyse_3.6_test/bin/python | + | $ /home/share/unix_files/cdat/ |
>>> | >>> | ||
>>> | >>> | ||
'/ | '/ | ||
>>> | >>> | ||
- | '/modfs/modtools/miniconda3//envs/analyse_3.6_test/ | + | '/home/share/unix_files/cdat/ |
Line 104: | Line 107: | ||
</ | </ | ||
+ | |||
+ | === Example: system independent paths with pathlib === | ||
+ | |||
+ | Note: the following example was generated on a Linux server and uses a <wrap em>/</ | ||
+ | |||
+ | < | ||
+ | >>> | ||
+ | PosixPath('/ | ||
+ | >>> | ||
+ | >>> | ||
+ | PosixPath('/ | ||
+ | >>> | ||
+ | True | ||
+ | >>> | ||
+ | False | ||
+ | >>> | ||
+ | [PosixPath('/ | ||
+ | >>> | ||
+ | [' | ||
+ | </ | ||
=== Example: getting the size(s) of all the files in a directory === | === Example: getting the size(s) of all the files in a directory === |
other/python/misc_by_jyp.txt · Last modified: 2024/11/04 15:01 by jypeter