User Tools

Site Tools


other:python:misc_by_jyp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
other:python:misc_by_jyp [2021/07/07 13:07]
jypeter Added the paths/filenames section
other:python:misc_by_jyp [2021/07/15 13:29]
jypeter [Working with paths and filenames] Added shutil and and example
Line 49: Line 49:
     * Available since Python version 3.4     * Available since Python version 3.4
     * [[https://​docs.python.org/​3/​library/​pathlib.html#​correspondence-to-tools-in-the-os-module|Matching pathlib, and os or os.path functions]]     * [[https://​docs.python.org/​3/​library/​pathlib.html#​correspondence-to-tools-in-the-os-module|Matching pathlib, and os or os.path functions]]
 +  * [[https://​docs.python.org/​3/​library/​shutil.html|High-level file operations]]
 +
 +=== Example: getting the size(s) of all the files in a directory ===
 +
 +<​code>​$ cd /​data/​jypmce/​TestDir
 +$ ls -l
 +total 72
 +-rw-r--r-- 1 jypmce ipsl 18147 Jun 25  2012 get_TS_cmip5.py
 +-rw-r--r-- 1 jypmce ipsl 16152 Jun 21  2012 get_TS_cmip5.py~
 +-rw-r--r-- 1 jypmce ipsl 13954 Jul  3  2012 get_TS_cmip5_regular.py
 +-rw-r--r-- 1 jypmce ipsl 16539 Jun 22  2012 get_TS_cmip5_regular.py~</​code>​
 +
 +<​code>>>>​ os.chdir('/​data/​jypmce/​TestDir'​)
 +>>>​ print(os.getcwd())
 +/​data/​jypmce/​TestDir
 +>>>​ files_list = os.listdir()
 +>>>​ files_list
 +['​get_TS_cmip5.py~',​ '​get_TS_cmip5_regular.py',​ '​get_TS_cmip5_regular.py~',​ '​get_TS_cmip5.py'​]
 +>>>​ files_sizes = list(map(os.path.getsize,​ files_list))
 +>>>​ files_sizes
 +[16152, 13954, 16539, 18147]
 +>>>​ sum(files_sizes)
 +64792</​code>​
 ==== Using command-line arguments ==== ==== Using command-line arguments ====
  
other/python/misc_by_jyp.txt · Last modified: 2024/04/19 12:02 by jypeter