Python应用之-file 方法

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# *_* coding=utf-8 *_*
 
"""
desc: 文件方法
 
 
#############################
file.read()         #read([size]) -> read at most size bytes, returned as a string.    
file.readline()     readline([size]) -> next line from the file, as a string.              
file.readlines()     
       readlines([size]) -> list of strings, each a line from the file.
        Call readline() repeatedly and return a list of the lines so read.
        The optional size argument, if given, is an approximate bound on the
        total number of bytes in the lines returned.
file.xreadlines()     xreadlines() -> returns self.
         
         
file.write()      write(str) -> None.  Write string str to file.
file.writelines()
           writelines(sequence_of_strings) -> None.  Write the strings to the file.
            Note that newlines are not added.  The sequence can be any iterable object
            producing strings. This is equivalent to calling write() for each string.
 
file.truncate()
      truncate([size]) -> None.  Truncate the file to at most size bytes.
      Size defaults to the current file position, as returned by tell().
       
file.seek()
       seek(offset [,whence])方法改变当前文件的位置。Offset变量表示要移动的字节数。From变量指定开始移动字节的参考位置。
                 如果from被设为0,这意味着将文件的开头作为移动字节的参考位置。如果设为1,则使用当前的位置作为参考位置。如果它被设为2,那么该文件的末尾将作为参考位置。
            
file.flush()            
file.tell()         current file position, an integer (may be a long integer).
file.next()             
                    
file.close()           
 
 
############不常用的方法#############
file.closed            
file.mode   
file.name              
    
file.isatty()              # true or false.  True if the file is connected to a tty device.          
file.readinto()             #不用用这个           
file.encoding                # 不常用              
file.errors                  # 不常用        
file.newline                 # 不常用        
file.softspace    
file.fileno()     
                fileno() -> integer "file descriptor".
                This is needed for lower-level file interfaces, such os.read().  
                   
###############################
version: 1.0
"""
 
fo= open("e:/temp.txt",'r')
print "文件名: ", fo.name
print "是否已关闭 : ", fo.closed
print "访问模式 : ", fo.mode
print "末尾是否强制加空格 : ", fo.softspace

  

posted @   圆柱模板  阅读(176)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示