Python: Difference between revisions

692 bytes added ,  28 April 2020
Line 88: Line 88:
# Note that splitext returns ("my_great_dataset.tar", ".gz")
# Note that splitext returns ("my_great_dataset.tar", ".gz")
</syntaxhighlight>
</syntaxhighlight>
If using Python >=3.4, you also have [https://docs.python.org/3/library/pathlib.html <code>pathlib</code>]
<syntaxhighlight lang="python">
from pathlib import Path
p = Path("my_folder")
# Join paths
pp = Path(p, "files.tar.gz")
pp.suffix # returns ".gz"
pp.suffixes # returns [".tar", ".gz"]
pp.name # returns "files.tar.gz"
pp.parent # returns "my_folder"
</syntaxhighlight>
;Notes
* One annoyance with <code>pathlib.Path</code> is that you need to convert things to strings manually
** This can be done with <code>str<code>, <code>.resolve()</code>, or <code>os.fspath()</code>
* [https://treyhunner.com/2019/01/no-really-pathlib-is-great/ "No really, pathlib is great" by Trey Hunger]


====List all files in a folder====
====List all files in a folder====