官术网_书友最值得收藏!

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:
主站蜘蛛池模板: 陕西省| 申扎县| 尼勒克县| 宣化县| 河南省| 敦煌市| 连南| 和田市| 鲁甸县| 哈密市| 大宁县| 安庆市| 合作市| 贺兰县| 彰武县| 苏州市| 宜丰县| 梨树县| 藁城市| 克山县| 轮台县| 淮阳县| 云浮市| 安仁县| 大理市| 湖南省| 棋牌| 兴义市| 依安县| 沙坪坝区| 二连浩特市| 巴中市| 永丰县| 兰坪| 西丰县| 宁德市| 辰溪县| 娱乐| 巴林左旗| 黔东| 房产|