Path.cwd()方法可以获取当前工作目录的路径。 frompathlibimportPathdefget_current_path():returnPath.cwd()print("当前工作目录:",get_current_path()) 1. 2. 3. 4. 5. 6. 3. 类图 为了更好地理解os和pathlib模块之间的关系,我们可以使用Mermaid语法来绘制一个类图。 classDiagram class os { +getcwd()...
def get_path(path_int): ''' :param path_int: 0表示获取当前路径,1表示当前路径的上一次路径,2表示当前路径的上2次路径,以此类推 :return: 返回我们需要的绝对路径,是双斜号的绝对路径 ''' path_count=path_int path_current=os.path.abspath(r".") # print('path_current=',path_current) path_c...
showcurpathlist(cur_path) os.rmdir('temp')# 删除创建的临时目录if__name__ =='__main__':print("当前系统平台: ", os.name)print("初始工作目录: ", getcurrentpath()) workpath =input("请输入工作目录: ")ifnotos.path.isabs(workpath):# 判断是否是绝对路径workpath = os.path.join(os.ge...
import os:导入os模块,这是一个Python标准库,提供与操作系统交互的功能。 current_path = os.getcwd():使用os.getcwd()函数获取当前工作目录,并将其赋值给current_path变量。 print("当前工作目录是:", current_path):输出当前工作目录的路径。 步骤4:运行脚本 在终端中,确保你已经进入到存放get_current_path.p...
一、Python OS 文件/目录方法 Python的os模块提供了与操作系统交互的方法,包括文件和目录的操作。以下是一些常用的os模块中的文件/目录方法: 目录操作 os.getcwd(): 返回当前工作目录的路径。 import os current_directory = os.getcwd() print(current_directory) os.chdir(path): 改变当前工作目录到指定的路径。
os.rmdir("/path/to/directory")获取文件属性:file_stats = os.stat("/path/to/file")删除文件:os.remove("/path/to/file")重命名文件:os.rename("/path/to/old_file", "/path/to/new_file")OS 高级用法 获取目录下的所有文件:import os# 获取目录下的所有文件defget_all_files_in_dir(dir_...
os.path.basename(path): 返回指定路径的文件名。 os.path.getsize(path): 返回指定路径的文件大小。 下面是一个简单的示例,演示了如何使用os.path模块来处理路径: import os # 获取当前工作目录 current_dir = os.getcwd() print("当前工作目录:", current_dir) # 拼接路径 new_path = os.path.join(cur...
一、os.getcwd() 获取当前工作目录,即当前Python脚本工作的目录路径。 代码示例: import os currentPath = os.getcwd() print ("当前工作目录:", currentPath) 二、os. chdir(path) 改变当前脚本工作目录;相当于shell下的cd命令。 代码示例: import os ...
from configparserimportConfigParserimportosclassReadConfigFile(object):defread_config(self):conn=ConfigParser()file_path=os.path.join(os.path.abspath('.'),'config_test.ini')ifnot os.path.exists(file_path):raiseFileNotFoundError("文件不存在")conn.read(file_path)url=conn.get('api','url')metho...
10.os.path.split()——返回路径的目录和文件名。例: 此处只是把前后两部分分开而已。就是找最后一个'/'。看例子: 11. os.getcwd()——获得当前工作的目录(get current work dir) 12. os.system()——执行shell命令。例: 注意:此处运行shell命令时,如果要调用python之前的变量,可以用如下方式: ...