在Python中,您可以使用os模块来操作路径。以下是一些常见的路径操作方法:
import oscurrent_dir = os.getcwd()print(current_dir)连接路径:import ospath1 = '/path/to/dir'path2 = 'file.txt'full_path = os.path.join(path1, path2)print(full_path)检查路径是否存在:import ospath = '/path/to/dir'if os.path.exists(path): print("Path exists")else: print("Path does not exist")创建目录:import osnew_dir = '/path/to/new/dir'os.mkdir(new_dir)列出目录下的文件和子目录:import ospath = '/path/to/dir'contents = os.listdir(path)for item in contents: print(item)这些是一些常见的路径操作方法,您可以根据您的需求进一步扩展和使用。


