This is the 13th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Review and

We learned the OS. Path module and pathlib module to operate file path in the front, unfamiliar partners can go to learn.

  • The Pathlib module started with PYTHon3.4 and has matured to Python3.6.

  • Pathblib has several advantages over the old OS. path:

    1. The imported modules of traditional operations are not unified. You can import both OS and OS. path, and the new usage can be managed using pathLib.

    2. It is difficult to switch traditional operations between different operating systems. Changing the operating system often requires code changes and often additional operations.

    3. The returned data type is different.

    (1) The traditional form is mainly function, and the data type returned is usually string.

    (2) However, paths and strings are not equivalent, so when using OS to operate paths, we often need to introduce other class libraries to assist in operation.

    (3) Pathlib module is object-oriented, more flexible and convenient to deal with.

    4. Pathlib simplifies many operations and makes it easier to use

In this installment, we’ll take a look at the Pathlib module approach versus the OS module approach, and see the differences between the OS module approach and the Pathlib module approach for the same operation

🎵🎵 Without further comment, Let’s learn the pathlib module and OS module related method comparison, Let’s go~

1. Obtain the current path

👉 OS module: Use the os.getcwd() function method to get the current path

Import os.path OS = os.getcwd() print(" OS path is: ", OS)Copy the code

👉pathlib module: Calls the CWD () method using the concrete Path object to get the current Path

Import pathlib Pa = pathlib.path () print("Pa Path ", pa.cwd ())Copy the code

2. Obtain the upper-level/upper-level directory

File directory structure display diagram:

👉 OS module: Use os.path.dirname to make nested calls to obtain the path of the upper-level and upper-level directories

Import os.path print("os.path module obtains upper layer directory: ",os.path.dirname(os.getcwd())) print("os.path module obtains upper layer directory: ",os.path.dirname(os.getcwd())) print("os.path module obtains upper layer directory: ",os.path.dirname(os.path.dirname(os.getcwd())))Copy the code

👉pathlib module: use the Path/PurePath object to call the parent property to get the character level directory

Import pathlib Pa = pathlib.path () print("pathlib get top directory :", pa.cwd ().parent) Print (" pa.cwd ().parent-parent ", pa.cwd ().parent-parent)Copy the code

3. Join directories

👉 OS module: use the join() method to call the dirname() method nested. Layers of calls, not easy to read

Import os.path print(" OS, path module split-path: ",os.path.join(os.path.dirname(os.getcwd()),"New","test1.txt")) import os.path print(" OS, path module split-path: ",os.path.join(os.path.dirname(os.getcwd()),"New","test1.txt"))Copy the code

👉pathlib module: use the parent property after calling join() to join the related path, simple and clear

Import pathlib Pa = pathlib.path () paths = ["New","test1.txt"] print("pathlib module Path: ", pa.cwd ().parent.joinPath (*paths))Copy the code

4. Create a folder and name it

👉 OS module: Creates file names at multiple levels, requiring complex nesting to be written

Import os.path # create JueJin/Test directory os.makedirs(os.path.join('JueJin', 'Test'), Exist_ok =True) # Rename test.txt to JueJin/ tests.txt os.rename('test.txt', os.path.join('JueJin', 'tests.txt '))Copy the code

👉pathlib module: Path objects can be directly created in the corresponding directory without writing nested calls

Pathlib. Path("JueJin/Test").mkdir(parents=True, exist_OK =True JueJin/Tests.txt pathlib.Path("test.txt").rename("Juejin/Tests.txt")Copy the code

5. Pathlib method diagram

Network transmission of a very popular Pathlib module method summary diagram, simple and intuitive, when you learn can have a look

6. Os. path and pathlib mapping table

operation os and os.path pathlib
An absolute path os.path.abspath Path.resolve
Modify the permissions os.chmod Path.chmod
Create a directory os.mkdir Path.mkdir
rename os.rename Path.rename
mobile os.replace Path.replace
Delete the directory os.rmdir Path.rmdir
Delete the file os.remove.os.unlink Path.unlink
Working directory os.getcwd Path.cwd
If there is a os.path.exists Path.exists
User directory os.path.expanduser Path.expanduser.Path.home
Directory or not os.path.isdir Path.is_dir
File or not os.path.isfile Path.is_file
Connection or not os.path.islink Path.is_symlink
File attributes os.stat Path.stat.Path.owner.Path.group
Whether it is an absolute path os.path.isabs PurePath.is_absolute
Path joining together os.path.join PurePath.joinpath
The file name os.path.basename PurePath.name
The higher the directory os.path.dirname PurePath.parent
The same file os.path.samefile Path.samefile
The suffix os.path.splitext PurePath.suffix

conclusion

In this installment, we learned how the Pathlib module compares with the os.path module to operate on the operating system path

The pathlib module is simpler and more convenient than the method provided by the os.path module, improving our code simplicity and maintainable

If your new project can use 3.6 or above directly, use PathLib.

See you next time ~ღ(´ · á´— · ‘) 🌹🌹🌹