Python 文件目录操作

File 操作

file.close()

# test.py

file = open("runoob.txt", "wb")
print("file name: ", file.name)
file.close()
[root@master python3_learning]# python3 test.py
file name:  runoob.txt

file.flush()

flush() 方法是用来刷新缓冲区的,即将缓冲区中的数据立刻写入文件,同时清空缓冲区,不需要是被动的等待输出缓冲区写入。

一般情况下,文件关闭后会自动刷新缓冲区,但有时你需要在关闭前刷新它,这时就可以使用 flush() 方法。

file.fileno()

fileno() 方法返回一个整型的文件描述符(file descriptor FD 整型),可用于底层操作系统的 I/O 操作。

# test.py

file = open("runoob.txt", "wb")
print("file name: ", file.name)
fid = file.fileno()
print("file descriptor: ", fid)
file.close()
[root@master python3_learning]# python3 test.py
file name:  runoob.txt
file descriptor:  3

file.isatty()

# test.py

file = open("runoob.txt", "wb")
print('is a tty? :', file.isatty())
file.close()
[root@master python3_learning]# python3 test.py
is a tty? : False

file.read()

read() 方法用于从文件读取指定的字节数,如果未给定或为负则读取所有。

# runoob.txt

first line
second line
third line
# test.py

file = open("runoob.txt", "r+")
print("file name: ", file.name)
line = file.read(10)
print("the string of reading: %s" % (line))
line = file.read()
print("the string of reading: %s" % (line))
file.close()
[root@master python3_learning]# python3 test.py
file name:  runoob.txt
the string of reading: first line
the string of reading: 
second line
third line

file.readline()

# runoob.txt

first line
second line
third line
# test.py

file = open("runoob.txt", "r+")
print("file name: ", file.name)
line = file.readline()
print("the first line of reading: %s" % (line))
line = file.readline(5)
print("the string of reading: %s" % (line))
file.close()
[root@master python3_learning]# python3 test.py
file name:  runoob.txt
the first line of reading: first line

the string of reading: secon

file.readlines()

# runoob.txt

first line
second line
third line
# test.py

file = open("runoob.txt", "r+")
print("file name: ", file.name)
line = file.readlines()
print("the result of reading: %s" % (line))
file.close()
[root@master python3_learning]# python3 test.py
file name:  runoob.txt
the result of reading: ['first line\n', 'second line\n', 'third line\n']

file.seek()

fileObject.seek(offset[, whence])
  • offset -- 开始的偏移量,也就是代表需要移动偏移的字节数,如果是负数表示从倒数第几位开始。

  • whence:可选,默认值为 0。给 offset 定义一个参数,表示要从哪个位置开始偏移;0 代表从文件开头开始算起,1 代表从当前位置开始算起,2 代表从文件末尾算起。

# test.py

f = open("workfile", "rb+")
f.write(b'0123456789abcdef')
print(f.seek(5))
print(f.read(2))
print(f.seek(-3, 2))
print(f.read(1))
[root@master python3_learning]# python3 test.py
5
b'56'
13
b'd'

file.tell()

# runoob.txt

first line
second line
third line
# test.py

file = open("runoob.txt", "r+")
print("file name: ", file.name)
pos=file.tell()
print('current position:%d' % (pos))
line = file.readline()
print("the result of reading: %s" % (line))
pos=file.tell()
print('current position:%d' % (pos))
file.close()
[root@master python3_learning]# python3 test.py
file name:  runoob.txt
current position:0
the result of reading: first line

current position:11

file.truncate()

# runoob.txt

first line
second line
third line
# test.py

file = open("runoob.txt", "r+")
print("file name: ", file.name)
file.truncate(8)
line = file.read()
print('the result of reading:%s' % (line))
file.close()
[root@master python3_learning]# python3 test.py
file name:  runoob.txt
the result of reading:first li

file.write()

# runoob.txt

first line
second line
third line
# test.py

file = open("runoob.txt", "r+")
print("file name: ", file.name)
# write a new line after the file end
file.seek(0, 2)
line = file.write('hello world')
file.seek(0, 0)
line = file.readlines()
print('the result of reading:%s' % (line))
file.close()
[root@master python3_learning]# python3 test.py
file name:  runoob.txt
the result of reading:['first line\n', 'second line\n', 'third line\n', 'hello world']

file.writelines()

# test.py

file = open("runoob.txt", "w")
print("file name: ", file.name)
seq = ['hello\n', 'world\n']
file.writelines(seq)
file.close()
[root@master python3_learning]# python3 test.py
file name:  runoob.txt
# runoob.txt

hello
world

 OS 操作

os.walk

# test.py

import os
for root, dirs, files in os.walk("/root/workspace/shell_learning", topdown=False):
    print(root, dirs, files)
[root@master shell_learning]# python3 test.py
/root/workspace/shell_learning/test1_dir [] ['out.txt']
/root/workspace/shell_learning/test2_dir/test1_dir [] ['out.txt']
/root/workspace/shell_learning/test2_dir ['test1_dir'] []
/root/workspace/shell_learning ['test1_dir', 'test2_dir'] ['passwd.txt', 'user.txt', 'get-docker.sh', 'textfile.txt', 'file1.txt', 'file2.txt', 'test.rb', 'textfile.txt.link', 'textfile2.txt', 'out.txt', 'test.txt', 'file3.txt', 'out.txt.link', 'test.sh']

 

to be continued

你可能感兴趣的:(Python,Python,文件操作,目录操作)