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

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:
主站蜘蛛池模板: 康乐县| 北京市| 峨眉山市| 长寿区| 鲁甸县| 昆明市| 华池县| 新化县| 辛集市| 宁远县| 当雄县| 富平县| 偃师市| 宜都市| 昌江| 新宁县| 资溪县| 兴海县| 利辛县| 济宁市| 玛多县| 营口市| 荣昌县| 曲阳县| 民权县| 乐至县| 兴义市| 辛集市| 彰化市| 通化县| 德格县| 新安县| 崇阳县| 平果县| 通河县| 盐山县| 景宁| 三江| 乌兰县| 若尔盖县| 崇阳县|