简要介绍 1.os库属于外部库,需要进行导入
2.一般情况下,os库在安装python解释器的时候就已经安装好了
具体函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 os.path.abspath(path) os.path.basename(path) os.path.commonprefix(list ) os.path.dirname(path) os.path.exists(path) os.path.lexists os.path.expanduser(path) os.path.expandvars(path) os.path.getatime(path) os.path.getmtime(path) os.path.getctime(path) os.path.getsize(path) os.path.isabs(path) os.path.isfile(path) os.path.isdir(path) os.path.islink(path) os.path.ismount(path) os.path.join(path1[, path2[, ...]]) os.path.normcase(path) os.path.normpath(path) os.path.realpath(path) os.path.relpath(path[, start]) os.path.samefile(path1, path2) os.path.sameopenfile(fp1, fp2) os.path.samestat(stat1, stat2) os.path.split(path) os.path.splitdrive(path) os.path.splitext(path) os.path.splitunc(path) os.path.walk(path, visit, arg) os.path.supports_unicode_filenames
使用例 比如说,我想把一个文件夹中的所有文件地址储存起来(方便直接在程序中调用)
1 2 3 4 5 6 7 8 9 10 11 import osfiles_address = [] origin_address = r"E:\....." list_address = os.listdir(origin_address) for i in list_address: file_path = os.path.join(origin_address, list_address[i]) if os.path.isfile(file_path): files_address.append(file_path)
有兴趣的可以参考一下这篇博客