标签:文件的 报错 remove python3 模块 sys open 目录文件 sts
import os print(os.name) 结果: nt
import os print(os.getcwd()) 结果: D:\python_workshop\python6\study
import os dirs = os.listdir(r"D:\python_workshop\python6") print(dirs) for file in dirs: print(file)
结果: [‘.idea‘, ‘alice.txt‘, ‘homeworks‘, ‘lessons‘, ‘personal_practices‘, ‘study‘, ‘url.txt‘] .idea alice.txt homeworks lessons personal_practices study url.txt
import os path = "./test" os.mkdir(path, 0o755) print("目录已创建")
结果: 目录已创建
import os path = "./test/android_app/auto_test" os.makedirs(path, 0o755) print("递归目录已创建")
结果: 递归目录已创建
import os path = "../" print("移除前的目录文件为:", os.listdir(path)) os.remove("../math.txt") print("移除后的目录文件为: ", os.listdir(path))
结果: 移除前的目录文件为: [‘.idea‘, ‘alice.txt‘, ‘homeworks‘, ‘lessons‘, ‘math.txt‘, ‘personal_practices‘, ‘study‘, ‘url.txt‘] 移除后的目录文件为: [‘.idea‘, ‘alice.txt‘, ‘homeworks‘, ‘lessons‘, ‘personal_practices‘, ‘study‘, ‘url.txt‘]
import os path = "../" print("删除前的目录文件为: ", os.listdir(path)) os.rmdir("../test") print("删除后的目录文件为: ", os.listdir(path)) 结果: 删除前的目录文件为: [‘.idea‘, ‘alice.txt‘, ‘homeworks‘, ‘lessons‘, ‘personal_practices‘, ‘study‘, ‘test‘, ‘url.txt‘] 删除后的目录文件为: [‘.idea‘, ‘alice.txt‘, ‘homeworks‘, ‘lessons‘, ‘personal_practices‘, ‘study‘, ‘url.txt‘]
import os print(os.environ.get("path")) 结果: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;D:\Program\python34;D:\Program\python34\Scripts;D:\Program\python34\Lib;D:\Program\python34\PyInstaller-3.3.1;D:\Program\python34\lib\site-packages\pywin32_system32
import os os.system(‘echo "hello world!"‘) 结果: "hello world!"
import os path = "D:\\python_workshop" name = "hello_world" #可以是不存在的目录或文件名 print(os.path.join(path, name)) 结果: D:\python_workshop\hello_world
import os path = "./url.txt" print(os.path.isfile(path)) path = "../python6" print(os.path.isdir(path)) 结果: False False
import os path = "../test" print(os.path.exists(path)) 结果: True
import os path = r"D:\python_workshop" print(os.path.split(path)) 结果: (‘D:\\‘, ‘python_workshop‘)
import os path = r"D:\python_workshop\python6\lesson1.py" print(os.path.splitext(path)) 结果: (‘D:\\python_workshop\\python6\\lesson1‘, ‘.py‘)
import os path = r"D:\python_workshop\python6" print(os.path.dirname(path)) 结果: D:\python_workshop
import os path = r"D:\python_workshop\python6" print(os.path.basename(path)) 结果: python6
import os name = r"D:\python_workshop\1.txt" print(os.path.getsize(name)) name = r"D:\python_workshop" print(os.path.getsize(name)) 结果: 197 4096
标签:文件的 报错 remove python3 模块 sys open 目录文件 sts
原文地址:https://www.cnblogs.com/cnhkzyy/p/8976076.html