- Hands-On Penetration Testing with Python
- Furqan Khan
- 194字
- 2021-07-02 14:14:01
Renaming and deleting files and accessing directories
In Python, system-level access to file directories and various other operating system commands is provided by the os module. The os module is a very powerful utility. In this section, we will see a few of its uses with respect to renaming, deleting, creating, and accessing directories with the help of the following snippet from the os_directories.py file:

The code snippet in the preceding screenshot shows the various ways in which the os module is used with files and directories in Python, in order to rename and delete files and create and change directories. It also showed us how we can rename and traverse all the files (including nested files) from a subfolder. It should be noted that if we wish to delete a folder, we can use the os.rmdir() method, but all the files of the folder should be explicitly deleted in order for this to work:
- The following output shows what happens with the file before and after its creation:

- The following output shows the change in the file name:

- The following output shows the change after the file is removed:

- Vue 3移動Web開發(fā)與性能調(diào)優(yōu)實戰(zhàn)
- iOS面試一戰(zhàn)到底
- Python快樂編程:人工智能深度學(xué)習(xí)基礎(chǔ)
- Three.js開發(fā)指南:基于WebGL和HTML5在網(wǎng)頁上渲染3D圖形和動畫(原書第3版)
- Practical Data Science Cookbook(Second Edition)
- Learning AWS Lumberyard Game Development
- 程序員考試案例梳理、真題透解與強化訓(xùn)練
- Expert Android Programming
- PhpStorm Cookbook
- Mastering JavaScript Design Patterns(Second Edition)
- Android系統(tǒng)級深入開發(fā)
- QGIS Python Programming Cookbook(Second Edition)
- 移動增值應(yīng)用開發(fā)技術(shù)導(dǎo)論
- Oracle數(shù)據(jù)庫編程經(jīng)典300例
- 和孩子一起學(xué)編程:用Scratch玩Minecraft我的世界