程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Self taught Python 38 OS object introduction

編輯:Python

Python OS Introduction of objects


List of articles

  • Python OS Introduction of objects
  • One 、OS Introduction of objects


stay Python In the program ,File Object can only operate on a certain file . But sometimes you need to operate on a folder directory , You need to use OS Object to implement . This time, only list OS The functions of , How to use them will be explained in detail in the next article .


One 、OS Introduction of objects

When operating a file in a computer system , It is inevitable to deal with folder directories . For some cumbersome file and directory operations , have access to Python Provided OS Module object . stay OS The module contains many functions to operate files and directories , It is convenient to rename files 、 add to / Delete directory 、 duplicate catalog / Documents and other operations .
stay Python In language ,OS The object mainly contains the following built-in functions , The introduction is shown in the following table .

The name of the function describe os.access(path, mode) Verify authority mode os.chdir(path) Change the current working directory os.chflags(path, flags) Set the path's flag as a numeric flag os.chmod(path, mode) Change permissions os.chown(path, uid, gid) Change file owner os.chroot(path) Change the root of the current process os.close(fd) Close the file descriptor fdos.closerange(fd_low, fd_high) Close all file descriptors , from fd_low( contain ) To fd_high( It doesn't contain ), Errors will be ignored os.dup(fd) Copy file descriptors fdos.dup2(fd, fd2) Put a file descriptor fd Copy to another fd2os.fchdir(fd) Change the current working directory through the file descriptor os.fchmod(fd, mode) Change access to a file , The file consists of parameters fd Appoint , Parameters mode yes UNIX File access rights under os.fchown(fd, uid, gid) Modify the ownership of a file , This function modifies the user of a file ID User group ID, This file is made up of file descriptors fd Appoint os.fdatasync(fd) Force file to disk , This file is made up of file descriptors fd Appoint , But don't force the status information of the file to be updated os.fdopen(fd[, mode[, buferingl]) Through file descriptors fd Create a file object , And return the file object os.fpathconf(fd, name) Return an open file system configuration information .name The value configured for the retrieved system , It may be a string that defines the system , These names are specified in many standards (POSIX.1, UNIX 95,UNIX 98 And others )os.fstat(fd)os.fstatvfs(fd)os.fsync(fd) Force the file descriptor to fd Of files written to the hard disk os.ftruncate(fd, length) Crop file descriptors fd Corresponding file , So it can't exceed the maximum file size os.getcwd() Return to the current working directory os.isatty(fd) If the file descriptor fd It's open , At the same time tty(-like) Equipment connection , Then return to True, otherwise Falseos.lchflags(path, flags) Set the path's flag as a numeric flag , It's like a function chflags(), But there are no soft links os.lchmod(path, mode) Modify the permissions of linked files os.Ichown(path,uid, gid) Change file owner , It's like a function chown(), But don't track links os.link(src, dst) Create a hard link , Named parameter dst, Point to parameters srcos.listdir(path) return path The specified folder contains a list of files or folder names os.lseek(fd, pos, how) Set the file offset position , The file consists of a file descriptor fd instructions . This function depends on the parameters how To determine the starting position of the file offset , Parameters pos The offset of the specified position os.lstat(path) for example stat(), But there are no soft links os.major(device) Extract the device from the original device number major number ( Use stat Medium st_dev perhaps st_rdev field)os.makedev(major,minor) With major and minor The equipment number forms an original equipment number os.makedirs(neme[, mode]) Recursive folder creation function . And the function mkdir() similar , But all that was created intermediate-level Folder needs to contain subfolders os.minor(device) Extract the device from the original device number minor number ( Use stat Medium st _dev perhaps st_rdev field)os.mkdir(path[, mode]) In numbers mode Of mode Create a file called path Folder , default mode yes 0777( octal )os.mkfifo(path[, mode]) Create named pipes ,mode Is the number , The default is 0666( octal )os.mknod(path[,mode=00600,device]) Create a file called path File system node os.open(path, flags[,mode]) Open a file , And set the required open options , Parameters mode It's optional os.openpty() Open a new pseudo terminal pair . return pty and tty File descriptor for os.pathconf(path, name) Return the system configuration information of related files os.pipe() Create a pipe , Returns a pair of file descriptors (r,W), It means reading and writing os.popen(cmd[,mode[, bufering]]) From a cmd Open a pipe os.read(fd, n) From file descriptor fd Read the most n Bytes , Returns the string containing the bytes read , File descriptor fd The corresponding file has reached the end , Returns an empty string os.readlink(path) Return the file pointed to by the soft link os.remove(path) The deletion path is nath If path It's a folder , Will throw out OSError, Check out rmdir( Delete one directoryos.removedirs(name) Recursively delete directories os.rename(src, dst) Rename a file or directory , from src To dstos.renames(old, new) Recursively rename the directory , You can also rename the document os.rmdir(path) Delete path Empty directory specified , If the directory is not empty , Then throw a OSError abnormal os.stat(path:) obtain path Specified path information , Function equivalent to C API Medium stat() system call os.stat_float_times([newvalue]) decision stat_result Whether or not to float Object display timestamp os.statvfs(path) Get file system statistics for the specified path os.symlink(src, dst) Create a soft link os.tcgetpgrp(fd) Return to terminal fd ( One by os.open() Open file descriptor returned ) Associated process groups os.tcsetpgrp(fd, pg) Settings and terminals fd ( One by os.open() Open file descriptor returned ) The associated process group is pgos.ttyname(fd) Returns a string , It represents with the file descriptor fd Associated terminal devices . If fd There is no associated terminal device , An exception is raised .os.unlink(path) Delete file path os.utime( path, times) Returns the specified path File access and modification time os. walk(top[, topdown=True[,onerror = None[, followlinks=False]]]) Output the file name in the folder by swimming in the number , Up or down os.write(fd, str) Write string to file descriptor fd in , Returns the length of the string actually written
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved