首页 > 文章列表 > python os.chdir()的使用

python os.chdir()的使用

Python os.chdir()
193 2022-08-07

1、os.chdir()用于改变当前工作目录到指定的路径。

2、语法为os.chdir(path)。

3、参数path,要切换到的新路径。

4、返回值,如果允许访问返回True,否则False。

实例

import os
os.chdir('G:/阿里云盘/音乐/')
datanames = os.listdir()
print(datanames)
for file in datanames:
    if ' - ' in file:
        new_name = file.replace(' - ', '')   
        os.rename(file, new_name)
        print(file + '  已改为  ' + new_name)
    else:
        pass

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。