Python自動化運維之4、格式化輸出、文件對象


Python格式化輸出:

Python的字符串格式化有兩種方式: 百分號方式、format方式

百分號的方式相對來說比較老,而format方式則是比較先進的方式,企圖替換古老的方式,目前兩者並存。[PEP-3101]

(1)百分號格式化

%[(name)][flags][width][.precision]typecode ....
  • (name) 可選,用於選擇指定的key
  • flags 可選,可供選擇的值有:

    + 右對齊;正數前加正好,負數前加負號;
    - 左對齊;正數前無符號,負數前加負號;
    空格 右對齊;正數前加空格,負數前加負號;
    0 右對齊;正數前無符號,負數前加負號;用0填充空白處

  • width 可選,占有寬度
  • .precision 可選,小數點后保留的位數
  • typecode 必選

    s,獲取傳入對象的__str__方法的返回值,並將其格式化到指定位置
    r,獲取傳入對象的__repr__方法的返回值,並將其格式化到指定位置
    c,整數:將數字轉換成其unicode對應的值,10進制范圍為 0 <= i <= 1114111(py27則只支持0-255);字符:將字符添加到指定位置
    o,將整數轉換成 八 進制表示,並將其格式化到指定位置
    x,將整數轉換成十六進制表示,並將其格式化到指定位置
    d,將整數、浮點數轉換成 十 進制表示,並將其格式化到指定位置
    e,將整數、浮點數轉換成科學計數法,並將其格式化到指定位置(小寫e)
    E,將整數、浮點數轉換成科學計數法,並將其格式化到指定位置(大寫E)
    f, 將整數、浮點數轉換成浮點數表示,並將其格式化到指定位置(默認保留小數點后6位)
    F,同上
    g,自動調整將整數、浮點數轉換成 浮點型或科學計數法表示(超過6位數用科學計數法),並將其格式化到指定位置(如果是科學計數則是e;)
    G,自動調整將整數、浮點數轉換成 浮點型或科學計數法表示(超過6位數用科學計數法),並將其格式化到指定位置(如果是科學計數則是E;)
    %,當字符串中存在格式化標志時,需要用 %%表示一個百分號

 

注意:%s, %d, %f這3個是非常常用的,當不確定用%s or %d,最好使用%s不會出錯

>>> d = {'x':32,'y':27.49325,'z':65}

>>> print('%(x)-10d %(y)0.3g' % d)
32 27.5

一些例子

tpl = "i am %s" % "tomcat"
 
tpl = "i am %s age %d" % ("tomcat", 18)
 
tpl = "i am %(name)s age %(age)d" % {"name": "tomcat", "age": 18}
 
tpl = "percent %.2f" % 99.97623
 
tpl = "i am %(pp).2f" % {"pp": 123.425556, }
 
tpl = "i am %.2f %%" % {"pp": 123.425556, }

 

(2)Format()方式:

[[fill]align][sign][#][0][width][,][.precision][type]
  • fill 【可選】空白處填充的字符
  • align 【可選】對齊方式(需配合width使用)
  • <,內容左對齊
  • >,內容右對齊(默認)
  • =,內容右對齊,將符號放置在填充字符的左側,且只對數字類型有效。 即使:符號+填充物+數字
  • ^,內容居中
  • sign 【可選】有無符號數字
    • +,正號加正,負號加負;
    • -,正號不變,負號加負;
    • 空格,正號空格,負號加負;
  • # 【可選】對於二進制、八進制、十六進制,如果加上#,會顯示 0b/0o/0x,否則不顯示
  • , 【可選】為數字添加分隔符,如:1,000,000
  • width 【可選】格式化位所占寬度
  • .precision 【可選】小數位保留精度
  • type 【可選】格式化類型
    • 傳入” 字符串類型 “的參數
      • s,格式化字符串類型數據
      • 空白,未指定類型,則默認是None,同s
    • 傳入“ 整數類型 ”的參數
      • b,將10進制整數自動轉換成2進制表示然后格式化
      • c,將10進制整數自動轉換為其對應的unicode字符
      • d,十進制整數
      • o,將10進制整數自動轉換成8進制表示然后格式化;
      • x,將10進制整數自動轉換成16進制表示然后格式化(小寫x)
      • X,將10進制整數自動轉換成16進制表示然后格式化(大寫X)
    • 傳入“ 浮點型或小數類型 ”的參數
      • e, 轉換為科學計數法(小寫e)表示,然后格式化;
      • E, 轉換為科學計數法(大寫E)表示,然后格式化;
      • f , 轉換為浮點型(默認小數點后保留6位)表示,然后格式化;
      • F, 轉換為浮點型(默認小數點后保留6位)表示,然后格式化;
      • g, 自動在e和f中切換
      • G, 自動在E和F中切換
      • %,顯示百分比(默認顯示小數點后6位)

一些例子:

tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
  
tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
  
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
  
tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
  
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
  
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
  
tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
  
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
  
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
  
tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
  
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
 
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
 
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
 
tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
 
tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)

更多格式化操作:https://docs.python.org/3/library/string.html

 

python文件對象

文件系統和文件:
  文件系統是OS用於明確磁盤或分區上的文件的方法和數據結構--即在磁盤上組織文件的方法
  計算機文件(或稱文件、電腦檔案、檔案)是存儲在某種長期存儲設備或臨時存儲設備中的一段數據流,並且歸屬於計算機文件系統管理之下

概括來講:
  文件是計算機中由OS管理的具有名字的存儲區域
  在Linux系統上,文件被看作是字節序列

 

batyes()這個內置函數,這個方法是將字符串轉換為字節類型

# utf-8 一個漢子:3個字節
# gbk一個漢子:2個字節, 1bytes = 8bit
# 字符串轉換字節類型:bytes(只要轉換的字符串,按照什么編碼)

s = "李納斯"
n = bytes(s,encoding="utf-8")
print(n)
n = bytes(s,encoding="gbk")
print(n)

# 字節轉化成字符串
new_str = str(bytes(s,encoding="utf-8"),encoding="utf-8")
print(new_str)

 

open()內置函數用於文件操作

操作文件時,一般需要經歷如下步驟:

  • 打開文件
  • 操作文件
  • 關閉文件

每次都要關閉文件很麻煩,with open('文件路徑','模式') as filename: 這種方式來打開文件。

#'r'為只讀,'1.txt'為文件路徑
f=open('1.txt','r',encoding='utf-8')  #打開文件
data=f.read()                #操作文件
f.close()                  #關閉文件
print(data)
 
# 用with語句打開,不需要自動關閉
with open('1.txt','r',encoding='utf-8') as f:
    print(f.read())

 

一、打開文件

文件句柄 = open(file, mode='r', buffering=-1, encoding=None)

打開文件時,需要指定文件路徑和以何等方式打開文件,打開后,即可獲取該文件句柄,日后通過此文件句柄對該文件操作。

打開文件的模式有:

  • r ,只讀模式【默認】
  • w,只寫模式【不可讀;不存在則創建;存在則清空內容;】
  • x, 只寫模式【不可讀;不存在則創建,存在則報錯】
  • a, 追加模式【可讀;   不存在則創建;存在則只追加內容;】

"+" 表示可以同時讀寫某個文件

  • r+, 讀寫【可讀,可寫】
  • w+,寫讀【可讀,可寫】
  • x+ ,寫讀【可讀,可寫】
  • a+, 寫讀【可讀,可寫】

 "b"表示以字節的方式操作

  • rb  或 r+b
  • wb 或 w+b
  • xb 或 w+b
  • ab 或 a+b

 注:以b方式打開時,讀取到的內容是字節類型,寫入時也需要提供字節類型,當以二進制的方式的效率比較高,因為磁盤底層是以字節存儲

二、操作

對於文件也是個迭代對象,所以循環遍歷文件的每一行用的非常多

f = open('/etc/passwd','r')
for line in f:
    print(line)
class file(object)
    def close(self): # real signature unknown; restored from __doc__
        關閉文件
        """
        close() -> None or (perhaps) an integer.  Close the file.
         
        Sets data attribute .closed to True.  A closed file cannot be used for
        further I/O operations.  close() may be called more than once without
        error.  Some kinds of file objects (for example, opened by popen())
        may return an exit status upon closing.
        """
 
    def fileno(self): # real signature unknown; restored from __doc__
        文件描述符  
         """
        fileno() -> integer "file descriptor".
         
        This is needed for lower-level file interfaces, such os.read().
        """
        return 0    
 
    def flush(self): # real signature unknown; restored from __doc__
        刷新文件內部緩沖區
        """ flush() -> None.  Flush the internal I/O buffer. """
        pass
 
 
    def isatty(self): # real signature unknown; restored from __doc__
        判斷文件是否是同意tty設備
        """ isatty() -> true or false.  True if the file is connected to a tty device. """
        return False
 
 
    def next(self): # real signature unknown; restored from __doc__
        獲取下一行數據,不存在,則報錯
        """ x.next() -> the next value, or raise StopIteration """
        pass
 
    def read(self, size=None): # real signature unknown; restored from __doc__
        讀取指定字節數據
        """
        read([size]) -> read at most size bytes, returned as a string.
         
        If the size argument is negative or omitted, read until EOF is reached.
        Notice that when in non-blocking mode, less data than what was requested
        may be returned, even if no size parameter was given.
        """
        pass
 
    def readinto(self): # real signature unknown; restored from __doc__
        讀取到緩沖區,不要用,將被遺棄
        """ readinto() -> Undocumented.  Don't use this; it may go away. """
        pass
 
    def readline(self, size=None): # real signature unknown; restored from __doc__
        僅讀取一行數據
        """
        readline([size]) -> next line from the file, as a string.
         
        Retain newline.  A non-negative size argument limits the maximum
        number of bytes to return (an incomplete line may be returned then).
        Return an empty string at EOF.
        """
        pass
 
    def readlines(self, size=None): # real signature unknown; restored from __doc__
        讀取所有數據,並根據換行保存值列表
        """
        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.
        """
        return []
 
    def seek(self, offset, whence=None): # real signature unknown; restored from __doc__
        指定文件中指針位置
        """
        seek(offset[, whence]) -> None.  Move to new file position.
         
        Argument offset is a byte count.  Optional argument whence defaults to
(offset from start of file, offset should be >= 0); other values are 1
        (move relative to current position, positive or negative), and 2 (move
        relative to end of file, usually negative, although many platforms allow
        seeking beyond the end of a file).  If the file is opened in text mode,
        only offsets returned by tell() are legal.  Use of other offsets causes
        undefined behavior.
        Note that not all file objects are seekable.
        """
        pass
 
    def tell(self): # real signature unknown; restored from __doc__
        獲取當前指針位置
        """ tell() -> current file position, an integer (may be a long integer). """
        pass
 
    def truncate(self, size=None): # real signature unknown; restored from __doc__
        截斷數據,僅保留指定之前數據
        """
        truncate([size]) -> None.  Truncate the file to at most size bytes.
         
        Size defaults to the current file position, as returned by tell().
        """
        pass
 
    def write(self, p_str): # real signature unknown; restored from __doc__
        寫內容
        """
        write(str) -> None.  Write string str to file.
         
        Note that due to buffering, flush() or close() may be needed before
        the file on disk reflects the data written.
        """
        pass
 
    def writelines(self, sequence_of_strings): # real signature unknown; restored from __doc__
        將一個字符串列表寫入文件
        """
        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.
        """
        pass
 
    def xreadlines(self): # real signature unknown; restored from __doc__
        可用於逐行讀取文件,非全部
        """
        xreadlines() -> returns self.
         
        For backward compatibility. File objects now include the performance
        optimizations previously implemented in the xreadlines module.
        """
        pass

2.x
2.x
class TextIOWrapper(_TextIOBase):
    """
    Character and line based layer over a BufferedIOBase object, buffer.
    
    encoding gives the name of the encoding that the stream will be
    decoded or encoded with. It defaults to locale.getpreferredencoding(False).
    
    errors determines the strictness of encoding and decoding (see
    help(codecs.Codec) or the documentation for codecs.register) and
    defaults to "strict".
    
    newline controls how line endings are handled. It can be None, '',
    '\n', '\r', and '\r\n'.  It works as follows:
    
    * On input, if newline is None, universal newlines mode is
      enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
      these are translated into '\n' before being returned to the
      caller. If it is '', universal newline mode is enabled, but line
      endings are returned to the caller untranslated. If it has any of
      the other legal values, input lines are only terminated by the given
      string, and the line ending is returned to the caller untranslated.
    
    * On output, if newline is None, any '\n' characters written are
      translated to the system default line separator, os.linesep. If
      newline is '' or '\n', no translation takes place. If newline is any
      of the other legal values, any '\n' characters written are translated
      to the given string.
    
    If line_buffering is True, a call to flush is implied when a call to
    write contains a newline character.
    """
    def close(self, *args, **kwargs): # real signature unknown
        關閉文件
        pass

    def fileno(self, *args, **kwargs): # real signature unknown
        文件描述符  
        pass

    def flush(self, *args, **kwargs): # real signature unknown
        刷新文件內部緩沖區
        pass

    def isatty(self, *args, **kwargs): # real signature unknown
        判斷文件是否是同意tty設備
        pass

    def read(self, *args, **kwargs): # real signature unknown
        讀取指定字節數據
        pass

    def readable(self, *args, **kwargs): # real signature unknown
        是否可讀
        pass

    def readline(self, *args, **kwargs): # real signature unknown
        僅讀取一行數據
        pass

    def seek(self, *args, **kwargs): # real signature unknown
        指定文件中指針位置
        pass

    def seekable(self, *args, **kwargs): # real signature unknown
        指針是否可操作
        pass

    def tell(self, *args, **kwargs): # real signature unknown
        獲取指針位置
        pass

    def truncate(self, *args, **kwargs): # real signature unknown
        截斷數據,僅保留指定之前數據
        pass

    def writable(self, *args, **kwargs): # real signature unknown
        是否可寫
        pass

    def write(self, *args, **kwargs): # real signature unknown
        寫內容
        pass

    def __getstate__(self, *args, **kwargs): # real signature unknown
        pass

    def __init__(self, *args, **kwargs): # real signature unknown
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __next__(self, *args, **kwargs): # real signature unknown
        """ Implement next(self). """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    buffer = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    closed = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    encoding = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    errors = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    line_buffering = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    name = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    newlines = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    _CHUNK_SIZE = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    _finalizing = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

3.x
3.x
f.close()        關閉文件
f.fileno()      返回文件描述符
f.readline()     從當前指針讀取一行
f.readlines()    從當前指針讀取到結尾的全部行
f.read()         從當前指針讀多少個字節,沒有參數讀取全部
f.tell()         告訴當前指針,是字節
f.seek(offset [whence])    移動指針,f.seek(0)把指針移動第一行第0個字節位置
    offset: 偏移量
    whence: 位置
        0: 從文件頭
        1:從當前位置
        2:從文件尾部
    
f.write(string)    打開文件時,文件不存在,r,r+都會報錯,其他模式則不會
f.writelines()     必須是字符串序列,把字符串序列當作一個列表寫進文件
f.flush()          在文件沒有關閉時,可以將內存中的數據刷寫至磁盤
f.truncate()       文件截取多少字節保留,指針后面的內容全部會清空

f.name             是返回文件名字,不是方法,是屬性    
f.closed           判斷文件是否已經關閉
f.encoding         查看編碼格式,沒有使用任何編碼,則為None
f.mode             打開文件的模式
f.newlines         顯示出換行符的,空為默認\n不顯示

一些例子:

#打開模式沒有b則打開的字符串
f  = open('db','r')
data = f.read()
print(data,type(data))
f.close()

#打開模式有b則打開的時字節
f1  = open('db','rb')
data = f.read()
print(data,type(data))
f.close()

# 打開模式有w和b則寫入的字符串需要轉換成字節
f2  = open('db','ab')
f.write(bytes("hello",encoding="utf-8"))
f.close()

f3
= open('db','a+',encoding="utf-8") # 如果打開模式無b,則read,按照字符讀取 data = f3.read(1) # tell當前指針所在的位置(字節) print(f3.tell()) # 調整當前指針你的位置(字節),寫的話會覆蓋原來的 f3.seek(f3.tell()) f3.write("777") f3.close()

 

三、管理上下文

為了避免打開文件后忘記關閉,可以通過管理上下文,即:

with open('log','r') as f:        
    ...

如此方式,當with代碼塊執行完畢時,內部會自動關閉並釋放文件資源。

在Python 2.7 及以后,with又支持同時對多個文件的上下文進行管理,即:

(1)讀取一個文件中的10行寫入另外一個文件中

with open('db1','r',encoding="utf-8") as f1,open('db2','w',encoding="utf-8") as f2:
    times = 0
    for line in f1:
        times += 1
        if times <= 10:
            f2.write(line)
        else:
            break

(2)將一個文件一行一行讀取並批量替換並寫入另外一個文件

with open('db1','r',encoding="utf-8") as f1,open('db2','w',encoding="utf-8") as f2:
    for line in f1:
        new_str = line.replace('ales','st')
        f2.write(new_str)

(3)假設現在有這樣一個需求,有一個10G大的文件,如何拷貝到另一個文件中去?下面將講一下如何同時打開兩個文件進行處理,以及文件太大的時候如何讀取用with語句就可以同時打開兩個文件,一個讀,一個寫。假設1.txt文件有10G大,如果用read則一次性就將內容讀到內存中去了,這顯然不合適,如果用readline()的話雖然也可以讀一行,但是並不知道何時結束,但是可以用for循環讀取文件這個可迭代的對象,一行行的讀取。下面三行代碼就可以實現了

with open('1.txt','r',encoding='utf-8') as fread,open('2.txt','w') as fwrite:
    for line in fread:          #一行行的讀
        fwrite.write(line)        #一行行的寫

 大量練習:

示例:    
>>> import os
>>> os.system('cp /etc/passwd /tmp/passwd')
>>> f1 = open('/tmp/passwd','r')

>>> type(f1)
>>> file

>>> f1.next()
>>> 'root:x:0:0:root:/root:/bin/bash\n'


示例:
>>> f1 = open('/tmp/passwd','r+')

>>> f1.next()
'root:x:0:0:root:/root:/bin/bash\n'

>>> f1.seek(0,2)

>>> f1.tell()
927

>>> f1.write('new line.\n')

>>> f1.tell()
937

>>> f1.close()


示例:帶+號可以避免文件不存在報錯
>>> import os
>>> os.system('cp /etc/shadow /tmp/shadow')

>>> f2 = open('/tmp/shadow','w+')

>>> f2.write('hello')

>>> f2.close()


示例:如果是只讀,文件不存在則報錯
>>> f3 = open('/tmp/test.txt','r')
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-12-d21a831010b6> in <module>()
----> 1 f3 = open('/tmp/test.txt','r')

IOError: [Errno 2] No such file or directory: '/tmp/test.txt'


注意:如果文件不存在,r,r+都會報錯


示例:
>>> f3 = open('/tmp/test.txt1','w+')

>>> for line in ( i**2 for i in range(1,11) ):
    f3.write(str(line)+'\n')
     

>>> f3.flush()

>>> f3.close()


示例:
>>> import os

>>> l2 = os.listdir('/etc/')

>>> f4.writelines(l2)

>>> f4.flush()

示例:
>>> l3 = [ i+'\n' for i in os.listdir('/etc/') ]
>>> f4.seek(0)
>>> f4.writelines(l3)
>>> f4.flush()

>>> f4.seek(0)
>>> f4.writelines([ i+'\n' for i in os.listdir('/etc/')])
>>> f4.flush()
View Code

 

文件系統功能:import os
目錄相關:
os.getcwd() 返回當前工作目錄
os.chdir() 切換目錄
os.chroot() 設定當前進程的根目錄
os.listdir() 列出指定目錄下的所有文件名
os.mkdir() 創建指定目錄
os.makedirs() 創建多級目錄
os.rmdir() 刪除陌路
os.removedirs() 刪除多級目錄

文件相關:
os.mkfifo() 創建管道文件
os.mknod() 創建設備文件
os.remove() 刪除文件

os.rename() 文件重命名
os.stat() 查看文件的狀態屬性

os.symlink() 創建鏈接文件
os.unlink() 刪除鏈接文件

os.utime() 更新文件時間戳
os.tmpfile() 創建並打開(w+b)一個新的
os.walk() 生成目錄結構的生成器

訪問權限:
os.access() 檢驗文件某個用戶是否有訪問權限
os.chmod() 修改權限
os.chown() 修改屬主屬組
os.umask() 設置默認權限模式

文件描述符:
os.open() 根據文件描述打開
os.read() 根據文件描述讀
os.write() 根據文件描述符寫

創建設備:
os.mkdev() 創建設備文件
os.major() 獲取設備主版本號
os.minor() 獲取設備次版本號


用戶相關:
os.getuid() 獲取當前用戶的uid
os.getgid() 獲取當前用戶的gid


文件路徑:
os.path.basename() 路徑基名
os.path.dirname() 路徑目錄名
os.path.join() 將dirname()和basename()連接起來
os.path.split() 返回dirname(),basename()元組
os.path.splitext() 返回(filename,extension)元組

os.path.getatime()
os.path.getctime()
os.path.getmtime()
os.path.getsize() 返回文件的大小

os.path.exists() 判斷指定文件是否存在
os.path.isabs() 判斷指定的路徑是否為絕對路徑
os.path.isdir() 判斷是否為目錄
os.path.isfile() 判斷是否為文件
os.path.islink() 判斷是否為鏈接文件
os.path.ismount() 判斷是否為掛載點
os.path.samefile() 判斷兩個路徑是否指向了同一個文件

對象持久存儲:
  pickle模塊
  marshal
  
DMB接口
shelve模塊


pickle.dump()
pickle.load()

示例:
>>> dir1 = os.path.dirname('/etc/sysconfig/network-scripts')

>>> file1 = os.path.basename('/etc/sysconfig/network-scripts')

>>> print(dir1,file1)
/etc/sysconfig network-scripts

>>> os.path.join(dir1,file1)
'/etc/sysconfig/network-scripts'


示例:
>>> for filename in os.listdir('/tmp'):
        print(os.path.join('/tmp',filename))


示例:判斷文件是否存在,存在則打開
讓用戶通過鍵盤反復輸入多行數據
追加保存至此文件中

#!/usr/bin/env python27

import os
import os.path

filename = '/tmp/test2.txt'

if os.path.isfile(filename):
    f1 = open(filename,'a+')

while True:
    line = raw_input('Enter somethin> ')
    if line == 'q' or line == 'quit':
        break
    f1.write(line+'\n')

f1.close()


示例:將字典寫入文件中,並還原
>>> import pickle
>>> d1 = {'x':123,'y':567,'z':'hello world'}
>>> f5 = open('/tmp/dfile.txt','a+')
>>> pickle.dump(d1,f5)
>>> f5.flush()
>>> f5.close()


>>> f6 = open('/tmp/dfile.txt','r')
>>> d2 = pickle.load(f6)
>>> print(d2)
{'y': 567, 'x': 123, 'z': 'hello world'}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM