Some of the functions we summarized in the previous module support advanced operations on files and directories. We will introduce how to use these functions in this section.
When we use files or directories, we are always inseparable from the rename operation. Sometimes we can choose to directly create an overwrite, but when our existing files store a large amount of information, we need to rename the operation. We use For the os.rename() method in the os module, students who use Pycharm can find that the name for manually renaming files in Pyacharm is also rename. The format of renaming in the program is:
os.rename(src,dst)
src is the changed file or directory name, and dst is the new name.
Look at the following example:
First create a folder:
importosprint(os.getcwd())#If the current directory is not known, output the current directory first. os.mkdir(r'C:UsersQingyanPycharmProjectsuntitled1teachnew')#Pay attention to this r. If there is such content as t,n, add r in front.
Then we perform the rename operation:
importosos.rename(r'C:UsersQingyanPycharmProjectsuntitled1teachnew',r'C:UsersQingyanPycharmProjectsuntitled1teachnewname')
We all use absolute paths when renaming. This method is more rigorous. It is better to add an 'r' in front. 'r' has been learned before and I will not introduce it too much here. Renaming files The same way as the directory, just pay attention to the suffix.
When deleting files, we use the os.remove() function in the os module. Its syntax format is:
os.remove(path)
Path is the path of the file we want to delete. You can use a relative path or an absolute path. The strict way to use it is to use an absolute path, because when the file that needs to be deleted is not in the current working directory, an exception will be thrown.
Look at the code below:
After running:
We used absolute paths and relative paths to delete two files in the current directory. What happens if we use relative paths to delete files in non-current directories.
The code is as follows:
importosos.remove('tst.py')
Running results:
Traceback(mostrecentcalllast):FileC:/Users/Qingyan/PycharmProjects/untitled1/teach/test.py,line2,in<module>os.remove('tst.py')FileNotFoundError:[WinError2]The system cannot find the specified document. :'tst.py'
Therefore, it is best to use absolute paths when deleting files:
importosos.remove(r'C:UsersQingyanPycharmProjectsuntitled1venvtst.py')
Many times we need to use programs to obtain some information about the current file, such as its last change time or file size.
We use the os.stat() method in the os module to obtain relevant information.
Let's learn how to use this method directly through examples:
importosfile=os.stat('test.py')print(os.path.abspath('test.py'))#Access path first print(file.st_dev)#Device number print(file.st_size)#File size print (file.st_atime)#Last access print(file.st_mtime)#Last modification time
The output is:
C:UsersQingyanPycharmProjectsuntitled1teachtest.py10109555972391580789484.6353131580789484.635313
This is all about the related operations of files and directories. No matter we are using C or C++ or Java language, project development cannot be separated from the management of files and directories. The content involved in actual development will be More, mastering these basic operations will be of great help to future studies.