Files
ailearning/docs/da/065.md
2020-10-19 21:08:55 +08:00

227 lines
3.9 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 与操作系统进行交互os 模块
`os` 模块提供了对系统文件进行操作的方法:
In [1]:
```py
import os
```
## 文件路径操作
* `os.remove(path)``os.unlink(path)` :删除指定路径的文件。路径可以是全名,也可以是当前工作目录下的路径。
* `os.removedirs`:删除文件,并删除中间路径中的空文件夹
* `os.chdir(path)`:将当前工作目录改变为指定的路径
* `os.getcwd()`:返回当前的工作目录
* `os.curdir`:表示当前目录的符号
* `os.rename(old, new)`:重命名文件
* `os.renames(old, new)`:重命名文件,如果中间路径的文件夹不存在,则创建文件夹
* `os.listdir(path)`:返回给定目录下的所有文件夹和文件名,不包括 `'.'``'..'` 以及子文件夹下的目录。(`'.'``'..'` 分别指当前目录和父目录)
* `os.mkdir(name)`:产生新文件夹
* `os.makedirs(name)`:产生新文件夹,如果中间路径的文件夹不存在,则创建文件夹
当前目录:
In [2]:
```py
os.getcwd()
```
Out[2]:
```py
'/home/lijin/notes-python/05\. advanced python'
```
当前目录的符号:
In [3]:
```py
os.curdir
```
Out[3]:
```py
'.'
```
当前目录下的文件:
In [4]:
```py
os.listdir(os.curdir)
```
Out[4]:
```py
['05.01 overview of the sys module.ipynb',
'05.05 datetime.ipynb',
'05.13 decorator usage.ipynb',
'.ipynb_checkpoints',
'05.03 comma separated values.ipynb',
'05.02 interacting with the OS - os.ipynb',
'05.10 generators.ipynb',
'05.15 scope.ipynb',
'05.12 decorators.ipynb',
'05.09 iterators.ipynb',
'my_database.sqlite',
'05.11 context managers and the with statement.ipynb',
'05.16 dynamic code execution.ipynb',
'05.14 the operator functools itertools toolz fn funcy module.ipynb',
'05.04 regular expression.ipynb',
'05.07 object-relational mappers.ipynb',
'05.08 functions.ipynb',
'05.06 sql databases.ipynb']
```
产生文件:
In [5]:
```py
f = open("test.file", "w")
f.close()
print "test.file" in os.listdir(os.curdir)
```
```py
True
```
重命名文件:
In [6]:
```py
os.rename("test.file", "test.new.file")
print "test.file" in os.listdir(os.curdir)
print "test.new.file" in os.listdir(os.curdir)
```
```py
False
True
```
删除文件:
In [7]:
```py
os.remove("test.new.file")
```
## 系统常量
当前操作系统的换行符:
In [8]:
```py
# windows 为 \r\n
os.linesep
```
Out[8]:
```py
'\n'
```
当前操作系统的路径分隔符:
In [9]:
```py
os.sep
```
Out[9]:
```py
'/'
```
当前操作系统的环境变量中的分隔符(`';'``':'`
In [10]:
```py
os.pathsep
```
Out[10]:
```py
':'
```
## 其他
`os.environ` 是一个存储所有环境变量的值的字典,可以修改。
In [11]:
```py
os.environ["USER"]
```
Out[11]:
```py
'lijin'
```
`os.urandom(len)` 返回指定长度的随机字节。
## os.path 模块
不同的操作系统使用不同的路径规范,这样当我们在不同的操作系统下进行操作时,可能会带来一定的麻烦,而 `os.path` 模块则帮我们解决了这个问题。
In [12]:
```py
import os.path
```
### 测试
* `os.path.isfile(path)` :检测一个路径是否为普通文件
* `os.path.isdir(path)`:检测一个路径是否为文件夹
* `os.path.exists(path)`:检测路径是否存在
* `os.path.isabs(path)`:检测路径是否为绝对路径
### split 和 join
* `os.path.split(path)`:拆分一个路径为 `(head, tail)` 两部分
* `os.path.join(a, *p)`:使用系统的路径分隔符,将各个部分合成一个路径
### 其他
* `os.path.abspath()`:返回路径的绝对路径
* `os.path.dirname(path)`:返回路径中的文件夹部分
* `os.path.basename(path)`:返回路径中的文件部分
* `os.path.slitext(path)`:将路径与扩展名分开
* `os.path.expanduser(path)`:展开 `'~'``'~user'`