This is the second day of my participation in the August More text Challenge. For details, see:August is more challenging

Review and

We learned the operation method of OS module related files and directories in the last issue, and have a basic grasp of it. Now we will further learn the sister module of OS module, OS. path.

Usually, we will study the OS module and OS. path module together.

Before we learn, let’s look at the difference between a directory and a path

  • Directory: The destination is the folder

    (1) The directory is unable to directly find the target file

    (2) There may be N files or folders in the directory

    (3) for example F:\Juejin\file\ Juejin the destination of the target is the Juejin folder

  • Path: The destination is the destination file.

    (1) The target file can be directly found according to the path

    (2) Paths are divided into absolute paths and relative paths

    (3) The end of the path is a file, called file path; The end of a path is a directory, called a directory path

    (4) For example, the path F:\Juejin\juejin. TXT points to the TXT file

💡 knock on the blackboard: path refers to the specific location, it can be a file 📃, can also be a folder 📁; The directory can only be 📁

💡 OS module: mainly operates files and directories. The os.path module focuses on path operations

1. Overview of the os.path module

The OS. Path module provides the common convenience of path judgment, path segmentation, path link, and folder traversal as follows:

methods describe
os.path.isabs(path) Check if path is an absolute path and return the Boolean type
os.path.isdir(path) Check whether path is a directory and return the Boolean type
os.path.isfile(path) Determines whether path is a file and returns the Boolean type
os.path.exists(path) Determines whether the file at the specified path exists. Returns the Boolean type
os.path.getsize(filename) Returns the size of the file
os.path.abspath(path) Return absolute path
os.path.dirname(path) Return the path of the directory
os.path.getatime(filename) Returns the last access time of the file
os.path.getmtime(filename) Return the time when the file was last modified
os.path.walk(top,func,arg) Traverse the directory recursively
os.path.join(path,*paths) Connecting multiple paths
os.path.split(path) The path is split and returned as a list
os.path.splitext(path) The extension of the file to split from the path

We in the CMD command window 🪟 for basic operations, as follows:

>>> import os
>>> import os.path
>>> os.path.isabs("F:\JueJin")
True
>>> os.path.isdir("F:\Juejin")
True
>>> os.path.isdir("F:\Juejin\a.txt")
False
>>> os.path.exists("F:\Juejin")
True
>>> os.getcwd()
'F:\\Juejin'
>>> os.path.exists("a.txt")
True
>>> os.path.getsize(r"F:\JueJin\a.txt")
0
>>> os.path.getatime(r"F:\Juejin\a.txt")
1627867023.051934
>>> os.path.getmtime(r"F:\JueJin\a.txt")
1627867023.051934
>>> os.path.getctime(r"F:\JueJin\a.txt")
1627867023.051934
Copy the code

2. Compare the OS module with the OS. path module

We have learned the so module before, and some methods of the os.path module are also overlaps with the OS module. Why does the os.path module exist?

With this in mind, let’s look at the difference between the OS module and the OS.path module

import os 
import os.path
Copy the code
  • OS module

Find the method in the os.py file in the module directory where you import OS to load Python

  • OS. The path module

If there is no path.py file in the Python module directory, how can the os.path module be loaded?

1. View the source code of os.py to view the code of the loaded module fragment

(1) If the operating system is Linux (‘ POSIX ‘), the path module is loaded by importing POSIxpath as PATH

(2) If Windows (‘nt’), the path module will be loaded by import ntPath as path

2. The Python interpreter loads os.path

(1) The Python interpreter first loads the underlying module, the OS module (2) the OS module condition determines whether the operating system type is window or Linux (3) the actual module is loaded and named path (4)sys.modules as the dictionary type of the current module, So from os.path import loads the specific method

The Python interpreter has been loaded to the OS module before the ✨🌟os.path module runs. Simply put, OS is a package, and PATH is a module within the package.

3. Experiment

Scenario 1: Generate a new file and save it in the specified folder

import os.path import time def Mylogger(path): Rq = time.strftime('%Y%m%d%H% m ', time.localtime(time.time())) filepath = os.path.dirname(path) Print (filepath) filename = rq+"logger"+".log" print(filepath) filename = rq+"logger"+".log" Mylogger(r"F:\JueJin\a.txt")Copy the code

The running results are as follows:

conclusion

In this installment, we provide methods for the os.path module to operate on directories. These functions can operate on the system directory itself. This module provides the exists() function to determine if the directory exists; The getctime(), getmtime(), and getatime() functions are also provided to get the time when the directory was created, last modified, and last accessed. The getSize () function is also provided to specify the size of the file and other methods ~

OS module and OS. Path module will frequently use the methods related to file directory and path in the operation of log file saving in daily work. I hope it can help you.

💪 We will continue to learn files related modules shutil, fileInputd and so on

Above is the content of this issue, look forward to the big guys to like the comments to correct ~