The following small series for everyone to bring a Python file name and file path operation instance. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
Readme:
In the daily work, we often involve in the operation of file name and file path, the OS standard module in Python provides us with various functions of file operation, this article will introduce "Get current path" "Get all files and folders under current path," Delete files "," delete directory/multiple directories " , "Check File/file path", "Check file path exists", "Detach file path and filename", "Detach File extension", "Get file name" and get "file path".
Import os ' Get current path ' ' CWD=OS.GETCWD () print (CWD)
"Get all files and folders under the current folder" Print (Os.listdir ())
' Delete file ' Os.remove (' Sw724.vaps ') print (Os.listdir ())
"' Delete a single directory and multiple directories ' ' Os.removedir () Os.removedir ()
"Check if File/folder" Print (Os.path.isfile ('/users/liuxiaolong/pycharmprojects/untitled/sw724.vaps ')) print ( Os.path.isdir ('/users/liuxiaolong/pycharmprojects/untitled/sw724.vaps '))
"' Check if file path exists ' Print (os.path.exists ('/USERS/LIUXIAOLONG/PYCHARMPROJECTS/UNTITLED/IIII '))
' Detach filename detach extension ' [Dirname,filename]=os.path.split ('/users/liuxiaolong/pycharmprojects/untitled/sw724.vaps ') Print (dirname, "\ n", filename) [Fname,fename]=os.path.splitext ('/users/liuxiaolong/pycharmprojects/untitled/ Sw724.vaps ') print (fname, "\ n", Fename)
"Get file path get filename get current Environment '" Print ("Get pathname:", Os.path.dirname ('/users/ Liuxiaolong/pycharmprojects/untitled/sw724.vaps ') print ("Get filename:", Os.path.basename ('/users/liuxiaolong/ Pycharmprojects/untitled/sw724.vaps ')) print (os.getenv)