And the old Ziko python toss the catalog _python

Source: Internet
Author: User

Python in the installation, it comes with a lot of modules, we call these modules standard library, which has a high frequency of use, is the OS. There are many methods and attributes in this library, and interested reader can refer to official documents: https://docs.python.org/2/library/os.html, or in interactive mode, with Dir (OS) to see.

Copy Code code as follows:

>>> import OS #这个动作很重要, cannot be missing
>>> dir (OS)
[' Ex_cantcreat ', ' ex_config ', ' ex_dataerr ', ' ex_ioerr ', ' ex_nohost ', ' ex_noinput ', ' ex_noperm ', ' ex_nouser ', ' EX_OK ', ' Ex_oserr ', ' ex_osfile ', ' ex_protocol ', ' ex_software ', ' ex_tempfail ', ' ex_unavailable ', ' ex_usage ', ' f_ok ', ' NGROUPS_ MAX ', ' o_append ', ' o_async ', ' o_creat ', ' o_direct ', ' o_directory ', ' o_dsync ', ' o_excl ', ' o_largefile ', ' o_ndelay ', ' o_ Noatime ', ' o_noctty ', ' o_nofollow ', ' o_nonblock ', ' o_rdonly ', ' o_rdwr ', ' o_rsync ', ' o_sync ', ' o_trunc ', ' o_wronly ', ' P_ NoWait ', ' P_nowaito ', ' p_wait ', ' r_ok ', ' seek_cur ', ' seek_end ', ' seek_set ', ' Tmp_max ', ' userdict ', ' wcontinued ', ' Wcoredump ', ' wexitstatus ', ' wifcontinued ', ' wifexited ', ' wifsignaled ', ' wifstopped ', ' Wnohang ', ' wstopsig ', ' WTERMSIG ' , ' wuntraced ', ' w_ok ', ' x_ok ', ' _environ ', ' __all__ ', ' __builtins__ ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' _copy_reg ', ' _execvpe ', ' _exists ', ' _exit ', ' _get_exports_list ', ' _make_stat_result ', ' _make_statvfs_result ', ' _ Pickle_stat_result ', ' _pickle_statvfs_result ', ' _spawnvef ', ' aBort ', ' access ', ' altsep ', ' chdir ', ' chmod ', ' chown ', ' chroot ', ' close ', ' closerange ', ' confstr ', ' confstr_names ', ' cterm Id ', ' curdir ', ' defpath ', ' devnull ', ' dup ', ' dup2 ', ' environ ', ' errno ', ' Error ', ' execl ', ' execle ', ' execlp ', ' execlpe ', ' Execv ', ' execve ', ' execvp ', ' execvpe ', ' extsep ', ' fchdir ', ' fchmod ', ' fchown ', ' fdatasync ', ' fdopen ', ' fork ', ' forkpty ', ' Fpathconf ', ' fstat ', ' fstatvfs ', ' fsync ', ' ftruncate ', ' getcwd ', ' getcwdu ', ' getegid ', ' getenv ', ' geteuid ', ' getgid ', ' ge Tgroups ', ' getloadavg ', ' getlogin ', ' getpgid ', ' getpgrp ', ' getpid ', ' getppid ', ' getresgid ', ' getresuid ', ' getsid ', ' Getuid ', ' initgroups ', ' isatty ', ' kill ', ' killpg ', ' lchown ', ' linesep ', ' link ', ' listdir ', ' lseek ', ' lstat ', ' major ', ' mak Edev ', ' makedirs ', ' minor ', ' mkdir ', ' mkfifo ', ' Mknod ', ' name ', ' nice ', ' open ', ' openpty ', ' pardir ', ' path ', ' pathconf ', ' Pathconf_names ', ' pathsep ', ' pipe ', ' popen ', ' popen2 ', ' popen3 ', ' popen4 ', ' putenv ', ' read ', ' Readlink ', ' Remove ', ' Remov Edirs ', ' Rename ', ' Renames ', ' RMDIR ', ' Sep ', ' setegid ', ' seteuid ', ' setgid ', ' setgroups ', ' setpgid ', ' setpgrp ', ' setregid ', ' setresgid ', ' setresuid ', ' Set Reuid ', ' setsid ', ' setuid ', ' spawnl ', ' Spawnle ', ' spawnlp ', ' spawnlpe ', ' spawnv ', ' spawnve ', ' spawnvp ', ' spawnvpe ', ' stat ', ' stat_float_times ', ' stat_result ', ' statvfs ', ' statvfs_result ', ' strerror ', ' symlink ', ' sys ', ' sysconf ', ' sysconf_ Names ', ' system ', ' tcgetpgrp ', ' tcsetpgrp ', ' tempnam ', ' Times ', ' tmpfile ', ' tmpnam ', ' ttyname ', ' umask ', ' uname ', ' unlink ', ' unsetenv ', ' urandom ', ' utime ', ' Wait ', ' wait3 ', ' wait4 ', ' waitpid ', ' walk ', ' write '

In so many things, this talk is only concerned about Os.path, really so-called "weak water 3,000, only take a scoop", why so favored it? Because it and previously talked about the file operation to cooperate, you can arbitrarily operate the files of various places (for documents, please refer to: Do not red-headed (1), do not red-headed (2))

There are also a number of properties about Os.path that can still be viewed with Dir (Os.path):

Copy Code code as follows:

>>> dir (Os.path)
[' __all__ ', ' __builtins__ ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' _joinrealpath ', ' _unicode ', ' _varprog ', ' Abspath ', ' altsep ', ' basename ', ' commonprefix ', ' curdir ', ' defpath ', ' devnull ', ' dirname ', ' exists ', ' expanduser ', ' Expandvars ', ' extsep ', ' genericpath ', ' getatime ', ' getctime ', ' getmtime ', ' getsize ', ' isabs ', ' isdir ', ' isfile ', ' Islink ', ' ismount ', ' join ', ' lexists ', ' normcase ', ' normpath ', ' os ', ' Pardir ', ' pathsep ', ' realpath ', ' relpath ', ' Samefil E ', ' sameopenfile ', ' samestat ', ' Sep ', ' Split ', ' splitdrive ', ' splitext ', ' stat ', ' supports_unicode_filenames ', ' sys ', ' Walk ', ' warnings ']

With so many properties, reader can use Help () to view the information one by one and understand how it is used. Here are a few common use methods, for reader to reduce the difficulty of reading English, however, if reader English is good enough, please read the original document directly. Just like this:

Copy Code code as follows:

>>> Help (Os.path.split)

Split (P)
Split a pathname. Returns tuple "(Head, tail)" where "tail" is
Everything after the final slash. Either part may is empty.

Here are some examples of typical illustrations:

Specifically, all of the following operations are entered into the following directory.

Copy Code code as follows:

qw@qw-latitude-e4300:~/documents/itarticles/basicpython/codes$ pwd
/home/qw/documents/itarticles/basicpython/codes #当前目录

qw@qw-latitude-e4300:~/documents/itarticles/basicpython/codes$ python

Python 2.7.6 (default, Nov 13 2013, 19:24:16)
[GCC 4.6.3] on linux2
Type ' help ', ' copyright ', ' credits ' or ' license ' for the more information.
>>>

Absolute path to File

Copy Code code as follows:

>>> Import Os.path
>>> Os.path.abspath ("225.py")
'/home/qw/documents/itarticles/basicpython/codes/225.py '

The file 225.py is actually present in the above path and gets the absolute path of the file. But what if you just provide a file that is not in this directory?

Copy Code code as follows:

>>> os.path.isfile ("225.py")
True

>>> os.path.isfile ("2222.py")
False
>>> Os.path.abspath ("2222.py")
'/home/qw/documents/itarticles/basicpython/codes/2222.py '

Os.path.isfile (path) can determine whether the path is a file, in fact, to determine whether there is a file in the path, if it exists, return true, or false. The above operation found that 2222.py this file does not exist in the current directory, but with Os.path.abspaht ("2222.py") it is possible to return an absolute path with a file name that does not exist. It may be understood here that if the file is to be created, it will be placed in that position.

In this understanding, you can also:

Copy Code code as follows:

>>> Os.path.abspath ("/home/qw/kkkkkkkk.kk")
'/home/qw/kkkkkkkk.kk '

Separate directories and file names

Copy Code code as follows:

>>> pn = Os.path.abspath ("225.py")
>>> PN
'/home/qw/documents/itarticles/basicpython/codes/225.py '

>>> Os.path.split (PN)
('/home/qw/documents/itarticles/basicpython/codes ', ' 225.py ')
>>> path, filename = os.path.split (PN) [0], Os.path.split (PN) [1]
>>> Path
'/home/qw/documents/itarticles/basicpython/codes '
>>> filename
' 225.py '

Os.paht.split (), the parameter is the directory plus file name, you can separate the path and file name. In fact, I think this function is not very intelligent, you see this

Copy Code code as follows:

>>> os.path.split ("/home/qw")
('/home ', ' QW ')

>>> os.path.split ("/home/qw/")
('/home/qw ', ')

It is the last group that is the file name, that is, the last/next is the filename, so the second experiment, the filename is empty. Isn't it a little silly?

Similarly, the parameters of the file or directory, not necessarily your computer is real, please see:

Copy Code code as follows:

>>> os.path.split ("/foo/python/qiwsir/git.git")
('/foo/python/qiwsir ', ' git.git ')

As long as the directory writing structure is met, it can be decomposed.

There are two other attributes that are executed separately by Os.path.split (), that is, the path and file name can be obtained separately, making the operation simpler.

Copy Code code as follows:

>>> os.path.dirname ("/foo/python/qiwsir/git.git")
'/foo/python/qiwsir '
>>> os.path.basename ("Foo/python/qiwsir/git.git")
' Git.git '

Judge

Earlier mentioned that Os.path.isfile () can be used to determine the existence of a file, then determine whether the directory path exists, can? Can

Copy Code code as follows:

>>> os.path.exists ("/foo/python/qiwsir")
False
>>> os.path.exists ("/home/qw/documents")
True

The related attributes are also determined:

Os.path.isabs (PATH): Determine if path is an absolute path
Os.paht.isdir (PATH): A directory that determines if path is present
Combining paths

It's common to combine two or more objects, so how do you combine multiple paths? As follows:

Copy Code code as follows:

>>> os.path.join ("/home/python", "/basicspython", "226.md")
'/basicspython/226.md '

In particular, the first absolute path is ignored in the return value of this property.

Copy Code code as follows:

>>> Os.path.join ("/", "/home/qw", "learnpython.md")
'/home/qw/learnpython.md '

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.