- 论坛徽章:
- 33
|
刚刚测试了一下, fileinput 确实是"直接"修改了文件中的行, 不过, 文件的inode变了, 说明在修改的时候, 应该是使用了临时文件之类的方法. 并不是真正意义上的直接修改.
- [user@host 14:56 /tmp/test_python]
- $ ls -li
- total 8
- 131 -rw-rw-r-- 1 user user 24 Feb 28 14:56 file1
- 132 -rw-rw-r-- 1 user user 137 Feb 28 14:56 test1.py
- [user@host 14:56 /tmp/test_python]
- $ cat file1
- line1
- line2
- line3
- line4
- [user@host 14:56 /tmp/test_python]
- $ cat test1.py
- #!/usr/bin/env python
- import fileinput
- for line1 in fileinput.input("file1", inplace=True):
- line1 = line1 + 'xxoo'
- print line1
- [user@host 14:56 /tmp/test_python]
- $ python test1.py
- [user@host 14:56 /tmp/test_python]
- $ ls -li
- total 8
- 130 -rw-rw-r-- 1 user user 44 Feb 28 14:56 file1
- 132 -rw-rw-r-- 1 user user 137 Feb 28 14:56 test1.py
- [user@host 14:56 /tmp/test_python]
- $ cat file1
- line1
- xxoo
- line2
- xxoo
- line3
- xxoo
- line4
- xxoo
- [user@host 14:57 /tmp/test_python]
- $
复制代码 |
|