os.path modular

Link to the original text :http://my.oschina.net/cuffica/blog/33494

  • basename(' File path ')    Remove directory path , return fname file name
import os
os.path.basename('/Volumes/1.mp4') # Output ('1.mp4')

  • dirname(' File path ')    Remove the filename , Return directory path
import os
os.path.dirname('/Volumes/1.mp4') # Output ('/Volumes')

  •  splitdrive(' File path ')    return (drivername,fpath) Tuples
import os
os.path.splitdrive('Volumes/1.mp4') # Output ('','/Volumes/1.mp4')

  • os.path.split(' File path ')    Split filename and path ; return (fpath,fname) Tuples ; If you use the directory completely , It also separates the last directory as a file name , And will not determine whether the file or directory exists
import os
os.path.split('/Volumes/1.mp4') # Output (‘/Volumes’,‘1.mp4’)
os.path.split('/Volumes/text') # Output (‘/Volumes’,‘text’)

  • os.path.splitext(“ File path ”)    Separating file names from extensions ; Default return (fname,fextension) Tuples , Slice operation
import os
fname, fextension=os.path.splitext('/Volumes/Leopard/Users/Caroline/Desktop/1.mp4')
print fname,fextension # Output /Volumes/Leopard/Users/Caroline/Desktop/1 .mp4
os.path.splitext('/Volumes/Leopard/Users/Caroline/Desktop/1.mp4')[1:] # Output ('.mp4',)

  • os.path.join('a','b','fname.extension')->'a/b/fname.extension'    Separate the parts into a pathname
imprort os
os.path.join('a','b','1.mp4') # Output #‘a/b/1.mp4’

Inquire about : Return value True,False

  • exists()       Specify the path ( File or directory ) Whether there is
  • isabs()         Specifies whether the path is absolute
  • isdir()         Specifies whether the path exists and is a directory
  • isfile()         Specifies whether the path exists and is a file
  • islink()       Specifies whether the path exists and is a symbolic link
  • ismount()   Specifies whether the path exists and is a mount point   ???
  • samefile()   Whether two pathnames point to the same file

file information

  • getatime()    Return to the most recent visit time   ( Floating point seconds )
  • getctime()    Returns the file creation time
  • getmtime()   Return the latest file modification time
  • getsize()       Return file size ( Bytes are units )
  • abspath()     Return to absolute path
  • normpath()   standard path String form   ???
import os
import time
file='/Volumes/Leopard/Users/Caroline/Desktop/1.mp4'
os.path.getatime(file) # Output the latest access time 1318921018.0
os.path.getctime(file) # Output file creation time
os.path.getmtime(file) # Output the latest modification time
time.gmtime(os.path.getmtime(file)) # With struct_time The form outputs the latest modification time
os.path.getsize(file) # Output file size ( Bytes are units )
os.path.abspath(file) # Output absolute path '/Volumes/Leopard/Users/Caroline/Desktop/1.mp4'
os.path.normpath(file) # Output '/Volumes/Leopard/Users/Caroline/Desktop/1.mp4'

[ turn ]Python os.path More articles on modules

  1. 【308】Python os.path Module common methods

    Reference resources :Python os.path modular Reference resources :python3 in ,os.path Summary of common usage under the module 01   abspath Return the absolute path of a directory . 02   basename Returns the base name of a directory ...

  2. python os.path modular -- Reprint

    os.path.abspath(path) # Return to absolute path os.path.basename(path) # Return the filename os.path.commonprefix(list) # return list( Multiple paths ) ...

  3. [ Reprint ]python os.path modular

    os.path Module is mainly used to obtain the attributes of the file , It is often used in programming , Here are some common methods of this module . There are more ways to check official documents :http://docs.python.org/library/os.path.ht ...

  4. python:os.path Module common methods

    os.path Module is mainly used to obtain the attributes of the file , It is often used in programming , Here are some common methods of this module . There are more ways to check official documents :http://docs.python.org/library/os.path.ht ...

  5. python os.path Module commonly used method detailed explanation

    os.path Module is mainly used to obtain the attributes of the file , It is often used in programming , Here are some common methods of this module . There are more ways to check official documents :http://docs.python.org/library/os.path.ht ...

  6. python os.path Module commonly used method detailed explanation ( turn )

    from :https://www.cnblogs.com/wuxie1989/p/5623435.html os.path Module is mainly used to obtain the attributes of the file , It is often used in programming , Here are some common methods of this module . More square ...

  7. python os.path Module commonly used method detailed explanation ZZ

    os.path Module is mainly used to obtain the attributes of the file , It is often used in programming , Here are some common methods of this module . There are more ways to check official documents :http://docs.python.org/library/os.path.ht ...

  8. python os.path modular

    os.path Module usage : 1, os.path.basename() >>> os.path.basename('/share/Public/cmiao')'cmiao' basen ...

  9. python os.path Detailed explanation of module usage

    abspath Return the absolute path of a directory Return an absolute path. >>> os.path.abspath("/etc/sysconfig/selin ...

Random recommendation

  1. and Java Related books , If you want to be an architect, please collect it

    1.<<Effective Java Chinese Second Edition >> 2.<<Java Concurrent programming practice >> 3.<<Java The core technology ( The book first 8 edition ) volume I_ Basic knowledge of ...

  2. Python Realization ORM

    ORM That is to map a data table in the database to a class in the code , The fields of the table correspond to the properties of the class . Encapsulate basic operations such as adding, deleting, modifying, and querying as methods corresponding to classes , To write cleaner and more hierarchical code . Take query data as an example , The original way of writing is Python generation ...

  3. PowerDesigner15.1 Create models and generate annotated sql Operation manual

    from :http://blog.csdn.net/huiwenjie168/article/details/7824029 One . Creating models operation :file-->new Model… Shortcut key :ctrl ...

  4. servlet Operating the database

    Tools :myeclipse Database tools :mysql java ee Operating the database , First, import the database driver file , I use it mysql At first , A lot of people have the right code, but they just can't connect , The reason is that I forgot to import the driver file . My drive ...

  5. Java Generic knowledge points : Generic classes 、 Generic interfaces and generic methods

    There are many reasons for generics , And one of the most remarkable reasons , Just to create a container class . Generic classes Container class should be one of the most reusable class libraries . Let's look at how to define a container class without generics : public class Cont ...

  6. Excerpts and redrafts : Understand thoroughly JS Execution mechanism

    Net text : https://juejin.im/post/59e85eebf265da430d571f89 Concurrency model and event loop :https://developer.mozilla.org/zh-CN/do ...

  7. javaBean Related knowledge and application of

    javaBean JavaBean brief introduction JavaBean It's a kind of JAVA Reusable components written in language . For writing JavaBean, Class must be concrete and public , And a constructor with no parameters .JavaBean By providing a ...

  8. NGINX Macro notes ( Variable | To configure )

    Preface Every tool has its soul , As a PHP The programmer , We may only use a small part of it , This article gives you a better understanding of Nginx, Most of this chapter is a summary . translate . Arrangement , I hope you know nginx not only PHP Accessories of ...

  9. Memcache Unauthorized access vulnerability

    Memcached Distributed cache system , default 11211 Ports don't need a password to access , Hackers can access all the information in the database directly , Cause serious information leakage . 0X00 Memcache install 1. download Mencache Of ...

  10. How to make e-mail marketing platform become EDM Artifact ?

    Any e-mail marketing company hopes to get more than expected return on its investment , There is an open rate . The phenomenon that the click through rate and the accompanying trading volume can keep rising , Of course, we can get accurate reports by monitoring the feedback from various platforms . Of course , As an e-mail marketing platform ...