import os

import shutil

#os.rename(‘ text_backup 2.txt’,’ text_rename.txt’)# rename files

#os.remove(‘ filename ‘)# Delete the file. If it does not exist, an error will be reported

#os.mkdir(‘textCJ’)# Create a folder

#os.rmdir(‘textCJ’)# Delete folder, only empty directory

#os.mkdir(‘d:/python programming ‘)# os.mkdir(‘d:/python programming ‘)# os.mkdir(‘d:/python programming ‘)# os.mkdir

# OS. Rmdir (‘ d: / python programming)

#os.makedirs(‘d:/p/d/f’)# multilevel file creation

#shutil. RmTree (‘d:p/d/f’)# Shutil

Print (‘ current path :’,os.getcwd())#

#print(os.path)

# OS. Path. Join (OS) path) join (OS) getcwd (), ‘the eleventh day)) # path joining together

# Get a list of directories in Python, older version

#print(os.listdir(‘d:/’))

print(”15)

# the new version

With oss.scandir (‘D:/test/’) as f:#scandir with with the context manager automatically frees the resource after iterator traversal is complete

for item in f:

print(item.name)

print(”15)

f=’D:/test/’

for item in os.listdir(f):

If os.path.isfile(os.path.join(f,item)):#

Print (‘ output file only :’,item)

2. Basic operational commands

Methods to explain

Os.getcwd () gets the current working directory, which is the directory path where the Python script is currently working

Os. chdir(” dirname “) changes the working directory of the current script; Equivalent to shell under CD

Os.curdir returns the current directory: (‘. ‘)

Os.pardir gets the parent directory string name of the current directory :(‘… ‘)

Os. makedirs(‘ dir1/dir2 ‘) generates a multi-level recursive directory

Os.removedirs (‘ dirname1 ‘) Deletes the directory if it is empty and recurses to the directory above it, removes it if it is also empty, and so on

Os.mkdir (‘ dirname ‘) generates a single-level directory; Equivalent to mkdir dirname in the shell

Os.rmdir (‘ dirname ‘) Deletes a single level empty directory. If the directory is not empty, it cannot be deleted

Os. listdir(‘ dirname ‘) lists all files and subdirectories in the specified directory, including hidden files, and prints them as a list

Os.remove () Deletes a file

Os. rename(” oldname “, “new”) renames the file/directory

Os.stat (‘ path/filename ‘) to get file/directory information

OS. Sep path delimiter: “\” in Win, “/” in Linux

Line terminator used on current platform, “\t\n” in Win, “\n” in Linux

Os. pathsep A string used to split the path of a file

The os.name string indicates the current platform used. Winwww pizei, com – > ‘nt; Linux – > ‘posix’

Os. system(” bash command “) runs shell commands and displays them directly

Os. environ Gets system environment variables

Os.path.abspath (PATH) returns the absolute path normalized by PATH

Os.path.split (path) returns a split path into a directory and filename tuple

Os.path.dirname (PATH) returns the directory of PATH. This is actually the first element of os.path.split(path)

Os.path.basename (path) returns the last filename of the path. If the path ends with/or \, a null value will be returned. That’s the second element of os.path.split(path)

Os.path.exists (path) returns True if path exists; Returns False if path does not exist

Os.path.isabs (path) Returns True if path is an absolute path

Os.path.isfile (path) Returns True if path is an existing file. Otherwise return False

Os.path.isdir (path) Returns True if path is an existing directory. Otherwise return False

Os.path. join(PATH1 [, PATH2 [,…]]]) returns multiple paths combined. Parameters before the first absolute path are ignored

Os.path.getatime (path) returns the last access time of the file or directory to which path points

Os.path.getmtime (path) returns the last modified time of the file or directory to which path points

2.2 Time and DateTime modules

Time module

import time

Time.sleep (6) # The program pauses for 6s

Print (time.time())#

Print (time.localtime())# print(time.localtime())#

Print (time. Strftime (” % % Y – m – H: % d % % m: % % S week w “, the time the localtime ())) # formatted output

Export: Dalian Abortion Hospital

  

Insert the picture description here

Time formatting symbol

%b local simplified month name

%B local full month name

%c local corresponding date representation and time representation

%j A day in a year (001-366)

%p Local equivalence of A.M. or p.m

The number of weeks in a year (00-53) Sunday is the beginning of the week

Week (0-6), with Sunday as the beginning of the week

The number of weeks in a year (00-53) Monday is the beginning of the week

%x local corresponding date representation

%X local corresponding time representation

%Z The name of the current time zone

The %% %% number itself

Datatime module

Mainly used for time calculation

Datetime. Date. Fromtimestamp (time. Time ()) # in time stamp

# Output: 2021-06-03

Addition and subtraction time calculation

import datetime

print(datetime.datetime.now())

# Current time plus 1 day, 2 hours and 30 minutes.

print(datetime.datetime.now() + datetime.timedelta(days=1,hours=2,minutes=30))

Output: