# -*- coding: utf-8 -*-"" "created on mon jan 9 00:35:43 2017@author-weixin: 953558077 "" "Import osimport shutilimport time" Introduction to basic features of the Shutil module CopyFile ( SRC, DST) copy from source Src to DST. The premise is, of course, that the destination address has writable permissions. The exception message thrown is ioexception. if the current DST already exists, it will be overwritten copymode ( SRC, DST) Just copy its permissions other things are not copied copystat ( SRC, DST) copy permissions, last access time, Last modified copy ( SRC, DST) copy a file to a file or a directory copy2 ( SRC, DST) copy files based on copy the last access time and modification time are also copied. Something similar to Cp –p copy2 ( SRC, DST) If the file system in two locations is the same as the rename operation, just rename it, or move if it is not on the same file system copytree (olddir,newdir,true/flase) copies a copy of the Olddir Newdir, if the 3rd parameter is true, the duplicateDirectory is kept in a symbolic connection under the folder, if the 3rd parameter is false, a physical copy will be generated in the replicated directory instead of the symbolic connection ' #创建一个文件夹os. mkdir (' Test ') #切换到文件夹下os. ChDir (' Test ') # The Intercept time function is used as the file name using Time = time.strftime ('%y-%m-%d ', Time.localtime ()) #在Test文件夹下创建一个文件file =open (time+ ' . txt ', ' W ') #打印当前所处的文件夹位置print (OS.GETCWD ()) #将当前文件夹的1. txt file to copy a copy of Shutil.copy (' 1.txt ', ' test/1.txt ') to the Test directory Move the current directory's 1.txt file to the Test directory under Shutil.move (' 1.txt ', "test/" + time + ' -1.txt ') #删除文件os. Remove (' test/') # Empty the contents of the file File.truncate () file.close () #删除文件夹及文件夹, before deleting this directory, all its contents should first be cleared Os.rmdir (' Test ')
This article is from the "Dimension Learning" blog, please be sure to keep this source http://9041438.blog.51cto.com/9031438/1890274
Python-Create a directory, switch directories, create files, copy files, move files, delete folders and files