2.python向文件中追加文本並打印出文件內容:python
>>> def add_some_text():ide
a=open('d:\\test01.txt','a')spa
a.write('\n111111')rem
>>> add_some_text()it
>>> class
>>> test
>>> f=open('d:\\test01.txt','r')import
>>> text=f.read()file
>>> print(text)im
This is a test to create a new text file
111111
>>>
3.在D盤下建立一個名爲PythonTest的文件夾:
>>> import os
>>> os.mkdir('d:\\Pythontest')
4.將D盤下的文件test01.txt複製到PythonTest目錄下:
>>> shutil.copy('d:\\test01.txt','d:\\PythonTest\\')
'd:\\PythonTest\\test01.txt'
複製時也可使用新的文件名,如:
>>> shutil.copy('d:\\test01.txt','d:\\PythonTest\\test02.txt')
'd:\\PythonTest\\test02.txt'
5.將文件test02.txt更名爲test03.txt
>>> shutil.move('d:\\PythonTest\\test02.txt','d:\\PythonTest\\test03.txt')
'd:\\PythonTest\\test03.txt'
6.刪除文件test03.txt:
>>> os.remove('d:\\Pythontest\\test03.txt')