python-fsutil is a Python package, which is used to make the file system easy with Python. This is one of the easiest file-system utilities ever built in python for working with file-system manipulation. python-fsutil is short for python file system utility.
With python-fsutil, we can perform different operations on the file systems in a very easy way using the python programming language.
Basic Features of python-fsutil:-
- python-fsutil is well tested.
- python-fsutil is Compatible with Python 2 and Python 3.
- python-fsutil is Simple and easy to use.
History of python-fsutil:-
python-fsutil is first created by Fabio Caccamo on Oct-28-2020. And is constantly updated by the developer. It has 62 stars and 3 forks and is currently hosted on GitHub.
How to install python-fsutil?
Installation of the python-fsutil is easy. we can install it by using pip
pip install python-fsutil
How to check if a Directory Exist or Not with Python?
Check it with python-fsutil
To check if a folder or a directory exists or not we will use the python-fsutil.assert_dir() function.
import fsutil validPath="D:\Coding_Programs\_01 Python\PythonWork5thSemester\AlixaProDev" print(fsutil.is_dir(validPath))
check it with the python os package.
We can do the same thing with OS Python built-in package the code to manually find if a directory exists or not is as follows.
import os validPath="D:\Coding_Programs\_01 Python\PythonWork5thSemester\AlixaProDev" print( os.path.isdir(validPath))
check it with stat package python.
We can also use the stat module along with the os module to go further close to the implementation to find if a directory exists or not.
import stat print(stat.S_ISDIR(os.stat(validPath).st_mode))
How to remove empty directories with Python:-
The following code snippets will help me to remove an empty directory that lies in the path.
import fsutil validPath="D:\Coding_Programs" fsutil.clean_dir(validPath, dirs=True, files=False)
How to copy a folder from one Place to anouther with Python?
Copying a directory with Python can be done in two lines of code. We have to use Python Package python-futil and we have to import the copy_dir() function. is you can see in the following code snippet.
import fsutil source="C:\\Downloads\\Android Apps" destination="C:\\Downloads\\desitnationfolder" fsutil.copy_dir(source, destination)
How to create a file with Python?
We can use the python package pthon-fsutil to create a file with the content specified. below is the code that will create a file with the “hello word this is AlixaProdev.com” text in it.
import fsutil fsutil.create_file ("./file.txt","hello world this is alixaprodev")
Or you can acheive the same result of creating the file with the text in it by using the built-in python function open(). this way you will be not using a third-party library to take help from while creating files. below is the code which uses the open() function to create a file with text in it. we have opened the file in write mode.
with open(".\\filename.txt", "w") as file: file.write("hello world this is Alixa")
how to create a zip file with python-fsutil?
Python python-fsutil package can help us create a zip-compressed file with the help of the following code
import fsutil validPathofcontent=".\\tobezipdir" fsutil.create_zip_file(".\\zipfilepath", validPathofcontent)
How to extract a zip file with Python?
with the help of python-fsutil we can easily extract a zip file. the following two lines of code can help us extract a zip file.
import fsutil zipfile=".\\tobezipdir.zip" fsutil.extract_zip_file(zipfile, validPathofcontent)
How to find the date of a folder creation?
Python-fsutil help us find the date of the creation of a directory. to get the date of the directory we can use the following code.
import fsutil foldername=".\\timefolder" date = fsutil.get_dir_creation_date(foldername)
How to find the size of a Directory with Python?
Another important function that is present in python-fsutil is to find the size of the directory. It returns the size of the directory or folder in bytes. look at the below code I have two files in the folder named directory. The size of both files is about 2432 bytes.
import fsutil folderpath=".\\folder" size = fsutil.get_dir_size(folderpath) print(size)
How to search for a file in a Directory with Python?
Searching is easy with the Python file system utility package python-fsutil which will help us search for a specific file or a group of files. for example, if I am looking for all the python files in the current directory I will use the following code.
import fsutil pythonfiles = fsutil.search_files(".", "*.py") print(pythonfiles)
Summary and Conclusion:-
We have learned about python-fsutil which is a library specially designed for lazy people. We have learned about different methods present in the package. If you are interested in other python tutorials please visit my youtube channel Code with Ali.