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

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:
主站蜘蛛池模板: 延川县| 项城市| 东辽县| 连江县| 海南省| 伊宁县| 台南市| 涞源县| 鄂伦春自治旗| 资兴市| 炎陵县| 天等县| 镇赉县| 金门县| 乾安县| 太保市| 疏勒县| 腾冲县| 淮阳县| 延川县| 白山市| 龙里县| 定南县| 礼泉县| 屏东县| 金山区| 浪卡子县| 六枝特区| 玉屏| 丹江口市| 九龙坡区| 永安市| 安吉县| 纳雍县| 九龙城区| 永城市| 辽宁省| 衡阳县| 景东| 常熟市| 梨树县|