This is a python3.5 of the file operations, there are copy/rename/overwrite input/append input and so on.
Premise: In the pythonaaa/a folder of e-drive, there is a 123.txt
1) Overwrite input
>>>import os #启动模块 >>>f=open ("E:/pythonaaa/a/123.txt", "w+") #打开目标文件, w+ is opened as read and write, with r+>>> F.write ("I love Beijing Cheonan") #写入内容就是歌词7 >>>f.close () #关闭文件, this step is crucial! Have to open about.
"Supplemental" If there is no 123.txt this file under the change path, then this will directly generate 123.txt this file.
This f=open way after opening the file must close (), otherwise this file will always run in the background, under Windows use Ctrl+alt+del can see this process, if you always feel that you will forget close (), you can use the with as method, The above sentence can be written like this:
>>>with Open ("E:/pythonaaa/a/123.txt") as F:f.write ("I love Beijing Tian ' an gate")
2) Append input
If the with as method opens the file, then write defaults to append input, such as
>>> with open ("E:/pythonaaa/a/123.txt", "A") as F:f.write ("\ nthe great leader Mao \ T" lead us forward ") #\n and \s effect to see for themselves
The effect is as follows:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7F/AF/wKiom1corYHztJdSAAAhusfCu0w616.png "title=" 1.png " alt= "Wkiom1coryhztjdsaaahusfcu0w616.png"/>
If you want to use f= method, then the first line to F=open ("E:/pythonaaa/a/123.txt", "a"), where A is the meaning of Add.
3) file name change
If you want to change this 123.txt to 456.txt, it is very simple, a word can be done.
>>> Import os>>> os.rename ("E:/pythonaaa/a/123.txt", "E:/pythonaaa/a/456.txt")
This effect is immediate.
4) file copy
Want to copy E:/pythonaaa/a/123.txt to e:/pythonaaa/b This folder, also called 123.txt.
>>> Import shutil>>> shutil.copy ("E:/pythonaaa/b/123.txt", "E:/pythonaaa/a/123.txt")
The "supplemental" statement is premised on the need to have a folder B, otherwise, it will be an error.
5) Directory deletion
E:/PYTHONAAA/B/C This folder do not want to, delete it.
>>> Import shutil>>> shutil.rmtree ("e:/pythonaaa/b/c")
6) file deletion
E:/pythonaaa/b/456.txt This file does not want to, delete it.
>>> Import os>>> os.unlink ("E:/pythonaaa/b/456.txt")
The Os.remove effect is the same, as are the statements.
7) file cut and paste
E:/pythonaaa/b/123.txt clip Paste to E:/pythonaaa/a/123.txt.
>>> Import shutil>>> shutil.move ("E:/pythonaaa/b/123.txt", "E:/pythonaaa/a/123.txt")
8) Comprehensive Exercises
Request that all the. py files in the e:/pythonaaa/a be copied to the E:/ABC/CBA folder.
>>> import os>>> os.makedirs ("E:/ABC/CBA") #建立/abc/ CBA this folder, if it is a layer of folder Os.mkdir on the line >>> def copyfiles (Sourcedir,targetdir): For files in os.listdir (SourceDir): sourcefile = os.path.join (sourcedir,files) targetfile = os.path.join (targetdir,files) try: if os.path.isfile (SourceFile) and Sourcefile.find ('. Py ') >0: #可以试试不加 The consequences of >0 Shutil.move (Sourcefile,targetfile) except Filenotfounderror: &nBsp; print ("This folder does not exist, please check again!") ") >>> copyfiles (" e:/ Pythonaaa/b "," E:/ABC/CBA ")
Either there is no error output, or there will be how many. py files will return how many times "This folder does not exist, please recheck!" ”
This article is from "Life is waiting for Gordo" blog, please make sure to keep this source http://chenx1242.blog.51cto.com/10430133/1769866
Python3.5 operation of the file