python--Additional study notes (ii)

Source: Internet
Author: User

File processing
# in-file move the Seek () method can move a file pointer in a file to a different location, the offset byte represents a position offset relative to a location, the default is 0, the representation starts at the beginning of the file, 1 represents the current position, and 2 represents the end of the file. Seek () is often used to reset the file location pointer. eg.>>> f.name ' anaconda-ks.cfg ' >>> f.next () ' #version =devel\n ' >>> f.next () ' # System Authorization information\n ' >>> f.seek (0) >>> f.next () ' #version =devel\n ' text () method is for Seek () Method Supplement: He tells you where the current file pointer is located in the file-in bytes from the beginning of the file. # The other Fileno () method returns the descriptor for the open file, which is a shape that can be used on some underlying operations such as the OS Module (Os.read ()). eg.>>> f = open (' anaconda-ks.cfg ', ' R ') >>> print F.fileno () 3flush () method directly writes the data of the internal buffer to the file immediately, Instead of passively waiting for the output buffer to be written. Typically used to flush buffers before files are closed. Isatty () is a Boolean built-in function that returns False if the file is a class TTY device that returns true otherwise. The eg.>>> F.isatty () falsetruncate () method intercepts the file to the current file pointer position or to a given size, in bytes. The file can be emptied. # # newline character The print output automatically adds a newline character at the end and adds a comma after the statement to avoid this behavior. The ReadLine () and ReadLines () functions do not handle any whitespace characters in the line, so it is necessary to add a comma. If you omit a comma, you will have two newline characters, one of which is included with the input, and the other is automatically added by the print statement. eg.>>> print ' Hello world! '; print ' Yes, We can! ' Hello world! Yes, we can!>>> print ' Hello world! ',;p rint ' Yes, we can! ' Hello world! Yes, We can!## File built-in Properties file.closed # file closes file.name #文件名file. Mode #打开文件的方式 # # file Operation Os.rename (old, new) #重命名os. mkdir () #创建文件夹os. Remove () #删除文件os. ChDir () #切换文件夹os. GETCWD () #得到当前路径os. RmDir () #删除文件夹补充: * File processing Mkfifo ()/mknod () #创建命名管道/Create File System node remove ()/unlink () #删除文件rename ()/renames () #重命名文件 *stat () [including stat (), Lstat (), XStat ()] #返回文件信息eg .>>> Import os>>> Os.system (' ls ') anaconda-ks.cfg fan lustre-master.zip test.log0>>> os.stat (' anaconda-ks.cfg ') posix.stat_ Result (st_mode=33152, st_ino=9240863, st_dev=64768l, St_nlink=1, St_uid=0, St_gid=0, st_size=955, st_atime=1513158628 , st_mtime=1509810306, st_ctime=1509810306) symlink () #创建符号链接utime () #更新时间戳tmpfile () #创建并打开 (' W+b ') a new temporary File Walk () # Generate all file names under a directory tree * Directory/Folder ChDir ()/fchdir () #改变当前目录/Change the current working directory through a file descriptor Chroot #改变当前进程的根目录listdir () #列出指定目录的文件 * Access/permissions Access      () #检验权限模式chmod () #改变权限模式chown ()/lchown () #改变owner和group ID/function is the same but does not track link umask () #设置默认权限模式 * File descriptor Operation Open ()     #底层的操作系统open (for files, use the standard built-in open () function) read ()/write () #根据文件描述符读取/Write Data dup ()/dup2 () #复制文件描述符/functionally the same, but is copied to another file descriptor * device number Makedev () #从major和minor设备号创建一个原始设备号major ()/mino      R () #从原始设备号获得major/minor device number # # # Os.path The path name Access function in the module * Split basename () #去掉目标路径, return file name DirName () #去掉文件名, return directory path join () #将分离的各部分组合成一个路径名split () #返回 (DirName (), basename ()) tuple splitdrive () #返回 (Drivename,pathnam) tuple splittext () #返回 (filename, extension) tuple * Information getatime () #返回最近返问时间getctime () #返回文件创建时间getmtime () #返回文件最近修改时间getsize () #返     The size of the file back in bytes, eg. >>> os.path.getsize (' test.log ') 48* query exists () #指定路径 (file or directory) exists Isabs () #指定路径是否为绝对路径isdir () #指定路径是否存在且为 One directory Isfile () #指定路径是否存在且为一个文件islink () #指定路径是否存在且为一个符号链接ismount () #指定路径是否存在且为一个挂载点samefiel () #两个路径名是否指向一 Files such as eg. >>> os.path.exists (' Test.log ') true>>> os.path.isfile (' Test.log ') true>>> Os.path.isdir ( ' Test.log ') false>>> os.path.islink (' Test.log ') false>>> os.system (' cp test.log Test01.log ') 0> >> Os.syStem (' ls ') anaconda-ks.cfg fan lustre-master.zip test01.log test.log0>>> os.path.samefile (' Test.log ', '         Test01.log ') false## command-line arguments sys.argv is a list of command-line arguments Len (SYS.ARGV) is the number of command-line arguments sys.argv[0] is the name of the program # # related modules bz2 #访问BZ2格式的压缩文件csv #访问CSV文件filecmp #用于比较目录和文件gzip/zlib #读取GNU Zip (gzip) file (compression requires zlib module) ZipFile #用于读取ZIP归档文件的工具

python--Additional Learning notes (b)

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.