Working with paths and files

Working with paths and files

from pathlib import Path
current_path = Path.cwd()
current_path
WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python')
type(current_path)
pathlib.WindowsPath
current_path.exists()
True
[x for x in dir(current_path) if not x.startswith('_')]
['absolute',
 'anchor',
 'as_posix',
 'as_uri',
 'chmod',
 'cwd',
 'drive',
 'exists',
 'expanduser',
 'glob',
 'group',
 'home',
 'is_absolute',
 'is_block_device',
 'is_char_device',
 'is_dir',
 'is_fifo',
 'is_file',
 'is_mount',
 'is_reserved',
 'is_socket',
 'is_symlink',
 'iterdir',
 'joinpath',
 'lchmod',
 'lstat',
 'match',
 'mkdir',
 'name',
 'open',
 'owner',
 'parent',
 'parents',
 'parts',
 'read_bytes',
 'read_text',
 'relative_to',
 'rename',
 'replace',
 'resolve',
 'rglob',
 'rmdir',
 'root',
 'samefile',
 'stat',
 'stem',
 'suffix',
 'suffixes',
 'symlink_to',
 'touch',
 'unlink',
 'with_name',
 'with_suffix',
 'write_bytes',
 'write_text']
list_of_files_and_dirs = list(current_path.glob("*.*"))
list_of_files_and_dirs
[WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/.ipynb_checkpoints'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/00-Python.md'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/01-Python-examples-Copy1.ipynb'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/01-Python-intro.ipynb'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/02-Python-examples-II.ipynb'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/02-Python-functions.ipynb'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/03-Python-modules.ipynb'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/04-Python-working-with-paths-and-files.ipynb'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/l03_variables_operators_types.ipynb'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/l04_more_operators_and_conditionals.ipynb')]
for f in list_of_files_and_dirs:
    print(f.name)
.ipynb_checkpoints
00-Python.md
01-Python-examples-Copy1.ipynb
01-Python-intro.ipynb
02-Python-examples-II.ipynb
02-Python-functions.ipynb
03-Python-modules.ipynb
04-Python-working-with-paths-and-files.ipynb
l03_variables_operators_types.ipynb
l04_more_operators_and_conditionals.ipynb
list_of_files_and_dirs[0]
WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/.ipynb_checkpoints')
list_of_files_and_dirs[0].is_file()
False
list_of_files_and_dirs[0].is_dir()
True

Building paths

parent_path = current_path.parent
parent_path
WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials')
parent_path.joinpath('00-Python')
WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python')
parent_path.joinpath('00-Python').exists()
True
assert parent_path.joinpath('00-Python/00-Python.md').exists()
assert parent_path.joinpath('00-Python/00-Python.md').is_file()
parent_path.is_dir()
True

Task 1

Get a list of the files in the current directory

[x for x in list(current_path.glob('*.*')) if x.is_file()]
[WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/00-Python.md'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/01-Python-examples-Copy1.ipynb'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/01-Python-intro.ipynb'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/02-Python-examples-II.ipynb'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/02-Python-functions.ipynb'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/03-Python-modules.ipynb'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/04-Python-working-with-paths-and-files.ipynb'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/l03_variables_operators_types.ipynb'),
 WindowsPath('C:/Users/eeu227/Documents/PROJECTS/PhDEcon108/docs/02-Tutorials/00-Python/l04_more_operators_and_conditionals.ipynb')]