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

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:
主站蜘蛛池模板: 南岸区| 蓬莱市| 清河县| 江口县| 马公市| 连南| 廊坊市| 承德市| 浙江省| 松滋市| 莆田市| 贺州市| 莫力| 哈尔滨市| 垦利县| 丁青县| 河池市| 昌邑市| 新巴尔虎右旗| 章丘市| 田林县| 游戏| 香格里拉县| 武鸣县| 吉隆县| 望江县| 岐山县| 孝感市| 修文县| 明水县| 康定县| 平乡县| 绍兴市| 开远市| 德清县| 盐山县| 安福县| 叶城县| 陈巴尔虎旗| 通化县| 绥化市|