如何获取当前工作目录?
使用该函数获取当前工作目录。os.getcwd()
等同于类 Unix 操作系统的 pwd 命令和 Windows 操作系统的 cd
import os
print(os.getcwd())
#C:\Users\Sarper\
如何更改当前工作目录?
使用该函数将当前工作目录更改为指定的 .os.chdir(path)path
此函数的等效项是 cd
import os
print(os.getcwd())
os.chdir("C:\\Users\\Sarper\\Desktop")
print(os.getcwd())
# C:\Users\Sarper
# C:\Users\Sarper\Desktop
如何查看目录中的所有文件?
使用 查看目录中的所有条目。对于 Windows,此函数的等效项是 dir,对于类 Unix 操作系统,则与此函数的 ls 等效。os.listdir(path='')
无需指定是否要查看当前工作目录中的所有条目。pa
import os
print(os.listdir(path="C:\\Users\\Sarper\\Downloads"))
# ['Picture1.png', 'Picture2.png']
如何在 Python 中创建目录?
Use itos.mkdir(path)在指定路径下创建目录。
此函数的等效项是 Windows 和类 Unix 操作系统的 mkdir。
import os
os.chdir("C:\\Users\\Sarper\\Downloads")
print(os.listdir())
os.mkdir("New file")
print(os.listdir())
# ['desktop.ini', 'Picture1.png', 'Picture2.png']
# ['desktop.ini', 'New file', 'Picture1.png', 'Picture2.png']
如何在 Python 中递归创建目录?
使用该 函数以递归方式创建目录。os.makedirs(path)
不能用函数 like 创建所有目录,但可以创建目录。os.mkdir(path)projects\python\projects
import os
# See all entries in the cwd
os.listdir()
Making directories
try:
os.mkdir(r"projects\python")
except:
print("ERROR")
os.makedirs(r"projects\python")
# See what happens after creating a new directory
print(os.listdir())
# check if we create directory recursively
print(os.listdir(path=r"C:\Users\Sarper\Downloads\projects"))
# ['Picture1.png', 'Picture2.png']
# ERROR
# ['Picture1.png', 'Picture2.png', 'projects']
# ['python']
不能使用该函数递归创建目录。os.mkdir(path)
- 文件和路径操作
如何在python中连接路径
使用 以连接一个或多个路径组件。os.path.join(path, *paths)
import os
path = os.path.join('path1', 'path2', "file.txt")
print(path)
# path1\path2\file.txtt
如何检查python中是否存在路径
使用 检查 路径是否存在。os.path.exists(path)
import os
os.chdir(“C:\\Users\\Sarper\\Downloads”)
path = os.
path.join('path1', 'path2')
print(os.path.exists(path))os.makedirs(path)
print(os.path.exists(path))
# False
# True
如何在 Python 中检查路径是否为文件或目录?
使用 用于检查路径是否为文件;使用 用于查看路径是否为 目录。os.path.isfile(path)os.path.isdirectory(path)
这两个函数将返回一个布尔值或 。TrueFalse
import os
print("projects :")
print("Is a file:", os.path.isfile("C:\Users\Sarper\Downloads\projects"))
print("Is a directory:", os.path.isdir("C:\Users\Sarper\Downloads\projects"))
print("\nPicture1.png :")
print("Is a file:", os.path.isfile("C:\Users\Sarper\Downloads\Picture1.png"))
print("\tIs a directory:", os.path.isdir(r"C:\Users\Sarper\Downloads\Picture1.png"))
# projects :
# Is a file: False
# Is a directory: True
# Picture1.png:
# Is a file: True
# Is a directory: False
3.环境变量
如何在 Python 中获取环境变量?
使用 funciton 获取包含环境变量的字典。os.envrion
此函数的等效项是 Windows 的 SET 和类 Unix 操作系统的 env。
如何在 Python 中获取特定的环境变量?
使用 获取特定的环境变量。os.envrion.get('variable_name')
此函数在 Windows 中的等效项是 echo %VARIABLE%; 对于类 Unix 操作系统,它是
运行系统命令
使用该函数运行命令,这些命令可以在命令提示符下运行。os.system(command)
import os
os.system(r"cd")
# C:\Users\Sarper\PycharmProjects\PyGame
4.其它
如何在 Python 中删除文件?
使用该 函数删除文件。os.remove(path)
对于 Windows,此函数的等效项是 del,对于类 Unix 操作系统,则为 rm。
import os
os.chdir(r"C:\Users\Sarper\Downloads")
print(os.listdir())
os.remove("Picture1.png")
print(os.listdir())
# ['Picture1.png', 'Picture2.png', 'projects']
# ['Picture3.png', 'projects']
如何在 Python 中删除目录?
使用该 函数删除空目录。os.rmdir(path)
此函数的等效项是 Windows 和类 Unix 操作系统的 rmdir。
如何在 Python 中递归删除目录?
使用该 函数以递归方式删除目录。os.removedirs(path)
对于 Windows,此函数的等效项是 rmdir
如何在 Python 中重命名文件或目录?
使用该函数重命名文件或目录。os.rename(src, dst)
对于类 Unix 操作系统,此函数的等效项是 mv <source>
import os
os.chdir(r"C:\Users\Sarper\Downloads")
print(os.listdir())
os.rename(r"C:\Users\Sarper\Downloads\Picture2.png", r"C:\Users\Sarper\Downloads\Picture3.png")
print(os.listdir())
# ['Picture2.png', 'projects']
# ['Picture3.png', 'projects']