在Python中,os模块提供了许多与操作系统交互的功能。以下是一些常用的os模块用法:
1. 获取当前工作目录:
```python
import os
current_dir = os.getcwd()
print("当前工作目录:", current_dir)
```
2. 改变当前工作目录:
```python
import os
new_dir = "/path/to/new/directory"
os.chdir(new_dir)
print("新的工作目录:", os.getcwd())
```
3. 列出指定目录下的所有文件和文件夹:
```python
import os
dir_path = "/path/to/directory"
files_and_folders = os.listdir(dir_path)
print("目录下的文件和文件夹:", files_and_folders)
```
4. 创建新目录:
```python
import os
new_dir = "/path/to/new/directory"
os.makedirs(new_dir)
print("新创建的目录:", new_dir)
```
5. 删除文件:
```python
import os
file_path = "/path/to/file"
os.remove(file_path)
print("已删除文件:", file_path)
```
6. 重命名文件或目录:
```python
import os
old_name = "/path/to/old_name"
new_name = "/path/to/new_name"
os.rename(old_name, new_name)
print("已重命名:", old_name, "->", new_name)
```
7. 检查文件或目录是否存在:
```python
import os
file_path = "/path/to/file_or_directory"
exists = os.path.exists(file_path)
print("文件或目录是否存在:", exists)
```
8. 获取文件或目录的大小:
```python
import os
file_path = "/path/to/file_or_directory"
size = os.path.getsize(file_path)
print("文件或目录大小:", size, "字节")
```
9. 关闭文件:
```python
import os
file_path = "/path/to/file"
os.close(file_path)
print("已关闭文件:", file_path)
```
10. 获取环境变量:
```python
import os
env_var = os.environ.get("ENV_VAR_NAME")
print("环境变量值:", env_var)
```
11. 获取当前工作目录的绝对路径:
```python
import os
abs_path = os.path.abspath(os.getcwd())
print("当前工作目录的绝对路径:", abs_path)
```
12. 创建符号链接:
```python
import os
source = "/path/to/source"
target = "/path/to/target"
os.symlink(source, target)
print("已创建符号链接:", source, "->", target)
```
13. 遍历目录:
```python
import os
dir_path = "/path/to/directory"
for root, dirs, files in os.walk(dir_path):
print("当前目录:", root)
print("子目录:", dirs)
print("文件:", files)
```
14. 运行系统命令:
```python
import os
command = "ls -l"
os.system(command)
```
15. 修改文件或目录的权限:
```python
import os
file_path = "/path/to/file_or_directory"
permissions = os.stat(file_path).st_mode
os.chmod(file_path, permissions | 0o755)
print("已修改权限:", file_path)
```
16. 等待用户输入:
```python
import os
input("按任意键继续...")
```
17. 获取进程ID:
```python
import os
pid = os.getpid()
print("进程ID:", pid)
```
18. 生成唯一标识符:
```python
import os
import uuid
unique_id = str(uuid.uuid4())
print("唯一标识符:", unique_id)
```
19. 挂载磁盘分区:
```python
import os
mount_point = "/path/to/mount_point"
os.makedirs(mount_point, exist_ok=True)
os.system("mount /dev/sdXN /path/to/mount_point")
print("已挂载磁盘分区:", mount_point)
```
20. 卸载磁盘分区:
```python
import os
unmount_point = "/path/to/unmount_point"
os.system("umount /path/to/unmount_point")
print("已卸载磁盘分区:", unmount_point)
```
21. 获取当前系统的信息:
```python
import os
system_info = os.uname()
print("当前系统信息:", system_info)
```
22. 修改文件或目录的所有者和所属组:
```python
import os
file_path = "/path/to/file_or_directory"
owner = "new_owner"
group = "new_group"
os.chown(file_path, owner, group)
print("已修改所有者和所属组:", file_path)
```
23. 在指定目录下创建符号链接,并指向原文件:
```python
import os
source = "/path/to/source"
target = "/path/to/target"
link_name = "/path/to/link"
os.symlink(source, link_name)
os.symlink(link_name, target)
print("已创建指向原文件的符号链接:", link_name)
```
24. 检查文件是否被其他进程占用:
```python
import os
file_path = "/path/to/file"
is_locked = os.path.isfile(file_path + ".lock")
print("文件是否被占用:", is_locked)
```
25. 在指定目录下查找文件:
```python
import os
dir_path = "/path/to/directory"
search_term = "search_term"
found_files = [f for f in os.listdir(dir_path) if search_term in f]
print("找到的文件:", found_files)
```
26. 在指定目录下查找文件,并返回其绝对路径:
```python
import os
dir_path = "/path/to/directory"
search_term = "search_term"
found_files = [os.path.join(dir_path, f) for f in os.listdir(dir_path) if search_term in f]
print("找到的文件(绝对路径):", found_files)
```
27. 在指定目录下查找文件,并返回其相对路径:
```python
import os
dir_path = "/path/to/directory"
search_term = "search_term"
found_files = [os.path.join(dir_path, f) for f in os.listdir(dir_path) if search_term in f and not os.path.isabs(f)]
print("找到的文件(相对路径):", found_files)
```
28. 在指定目录下查找文件,并返回其完整路径:
```python
import os
dir_path = "/path/to/directory"
search_term = "search_term"
found_files = [os.path.join(dir_path, f) for f in os.listdir(dir_path) if search_term in f and os.path.isabs(f)]
print("找到的文件(完整路径):", found_files)
```
29. 在指定目录下查找目录,并返回其绝对路径:
```python
import os
dir_path = "/path/to/directory"
search_term = "search_term"
found_dirs = [os.path.join(dir_path, d) for d in os.listdir(dir_path) if search_term in d and os.path.isdir(os.path.join(dir_path, d))]
print("找到的目录(绝对路径):", found_dirs)
```
30. 在指定目录下查找目录,并返回其相对路径:
```python
import os
dir_path = "/path/to/directory"
search_term = "search_term"
found_dirs = [os.path.join(dir_path, d) for d in os.listdir(dir_path) if search_term in d and os.path.isdir(os.path.join(dir_path, d)) and not os.path.isabs(os.path.join(dir_path, d))]
print("找到的目录(相对路径):", found_dirs)
```
31. 在指定目录下查找目录,并返回其完整路径:
```python
import os
dir_path = "/path/to/directory"
search_term = "search_term"
found_dirs = [os.path.join(dir_path, d) for d in os.listdir(dir_path) if search_term in d and os.path.isdir(os.path.join(dir_path, d)) and os.path.isabs(os.path.join(dir_path, d))]
print("找到的目录(完整路径):", found_dirs)
```
以上是os模块的一些其他功能,你可以根据需要进行组合和调整。
以上是os模块的一些其他功能,你可以根据需要进行组合和调整。
以上是os模块的一些常用功能,你可以根据需要进行组合和调整。
标签:python,os,用法,import,print,path,OS,dir From: https://www.cnblogs.com/full-stack-linux-new/p/17658663.html