文件处理
open函数 buildin function
1 | open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)` |
File access modes
1 | Character Meaning |
Read a file
In Python 3, If files do not open in binary mode, the encoding will be determined by or user's input. 1
2
3
4
5
6
```python
>>> with open("/etc/hosts", encoding="utf-8") as f:
... content = f.read()
...
>>> print(type(content))
<class 'str'>
1 | ```python |
In python3 binary mode
1 | with open("/etc/hosts", "rb") as f: |
In python2
1 | ## The content of the file is a byte string, not a Unicode string. |
Write a File
1 | "Python is great!" file_content = |
Copy a file
1 | from distutils.file_util import copy_file |
Move a File
1 | from distutils.file_util import move_file |
Readline
1 | ## If you are not using linux machine try to change the file path to read |