Python file, directory, and path
You can get the file content as a list by [1] opening the file in the with statement and [2] using the readlines() method. python with open...
Create a directory or folder in Python
To create a new folder, use `mkdir()` in `os` module. python import os os.mkdir('new_folder') The `new_folder` directory is create...
Get all files with a certain extension in Python
The simplest way to get all files with `py` extension is using glob module in Python. python import glob a = glob.glob('*.py') b = glob...
Path.touch - Create a file in Python
Path class has `touch` method that creates a file. Path is in [pathlib](/python-current-path) module and useful to handle files and directories ...
Delete a file in Python - Let's use "unlink" function in pathlib
You can easily delete a file using pathlib. python from pathlib import Path p = Path(__file__).parent / 'data' / 'mail.txt' p.unlink(...
Differences of Path(), Path(__file__) and Path('..') in Python pathlib
Python Path.resolve returns the absolute path of a file/directory. python from pathlib import Path p1 = Path() print(p1.resolve()) # /...
Read a file using Path.read_text in Python - Good bye with-open-read code
Python Path.read_text function can substitute the traditional `with-open` way. python from pathlib import Path p = Path(__file__).parent...
Python path suffix (file extension)
Python Path module enables to get easily the suffix of a file. python from pathlib import Path p1 = Path(__file__) print(p1.as_posix(...
How to zip a file in Python: Why is "arcname" in "write" function important?
Python zipfile module has methods to make zip files. The below is the simplest code. python from zipfile import ZipFile, ZIP_DEFLATED wi...
Python json.dumps - How to write json data to a file in Python
To write data (as a Python list or dictionary) to a json file, the `json.dumps()` is needed to convert it to the string. python import json...
Python pathlib: How to get the files and directories in the current directory in Python
How to get all the items of the current directory in Python: python from pathlib import Path # current file file = Path(__file__) prin...
Python Path and PosixPath: How to get the current path of a file or directory
In Python, pathlib package is very useful and helpful to get the file/directory path. python from pathlib import Path p = Path(__file__)...
Python String
Python List
Python Tuple
Python Set
Python Dictionary
Python Float
Python Decimal
Python Fraction
Python Date & Time
Python Function
Python Class
Python File
Python Built in
Python Math
Python Counter
Python Functools
Python Itertools
Python Deque
Python Tips
© Rollpie