python learning -os introduced

# Introduced
import os

# Path processing - external resources -os

# Get the current path
workspace = getcwd function under os.getcwd () # os module
print (workspace)

# List all files in the current path and the folder
Files = os.listdir (Workspace)
Print (Files)

# File name and path of stitching together the path of joining the path of joining + path +. . . + File name
# [ '111.py', '11111 ', 'homework_20190417_ reference answer .py', 'os_practise.py', '__init__.py']
file3 = the os.path.join (Workspace, "Xiaojian", Files [2])
file3 = the os.path.join (Workspace, "\\", Files [2])
Print (file3)

# If file exists among the file system
# file3 = D: \ Pychram- Workspace \ python17 \ class_20190420 \ xiaojian \ homework_20190417_ Answers .py
RES = os.path.exists (file3)
Print (RES)


# Is not a file, to determine whether a file exists
print (os.path.isfile (file3))

# ### stitching path has split and divided, there is no direct link with the operating system's file system? ?
# String manipulation

#file3=D:\Pychram-Workspace\python17\class_20190420\xiaojian\homework_20190417_参考答案.py
file_list = os.path.split(file3) #
print(file_list)

# File_list = ( 'D: \\ Pychram-Workspace \\ python17 \\ class_20190420 \\ xiaojian', 'homework_20190417_ Answer .py')
# is not the path, the path is determined whether there is
print (os.path.isdir ( file_list [0])) # \\ -> \

# 'D: \\ \\ python17 Pychram-Workspace \\ \\ class_20190420 Xiaojian'
# # \ escape character D: \

Case # path does not exist, create a folder.
D #: \\ \\ python17 Pychram-Workspace \\ \\ class_20190420 Xiaojian
# Create a folder is created only last a folder, all levels of the previous end of the folder must already exist in the operating system which
# os.mkdir ( " D: \\ \\ python17 Pychram-Workspace \\ \\ class_20190420 Xiaojian ")
# os.mkdir (" D: \\ \\ python18 Pychram-Workspace \\ \\ class_20190420 Xiaojian ")

# Create a series of folders. Among the given directory, as long as it does not exist, it will be created.
# os.makedirs ( "D: \\ Pychram -Workspace \\ python18 \\ class_20190420 \\ xiaojian")

# When the path does not exist, it is created.
Not os.path.exists IF ( "D: \\ \\ python18 Pychram-Workspace \\ \\ class_20190420 Xiaojian"):
os.makdirs ( "D: \\ \\ python18 Pychram-Workspace \\ \\ class_20190420 Xiaojian" )
the else:
Print ( "path already exists, do not create !!")

"" "
Debug tips break point, F7, F8

Operation file path
module: os
. 1, to get the current working space: The os.getcwd ()
2, lists the file and folder names in the specified directory: os.listdir (directory)
3, spliced together the path and file: os .path.join (a, b)
4, to determine what path exists: os.path.exists (path)
5, determine what path is a directory or a file path does
os.path.isdir (a) os.path .isfile (A)
6, if the path does not exist, create a path.
Finally, create a path os.mkdir (a)
create all os.makedirs path does not exist (b)

"""

Guess you like

Origin www.cnblogs.com/qsmyjz/p/11261223.html