Example of file path operation in Python

Source: Internet
Author: User
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) 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.