python中文件输入输出及os模块对文件系统的操做

一、内建函数open(file_name,文件打开模式,通用换行符支持),打开文件返回文件对象。html

二、对打开文件进行读取时,readline()与readlines()的区别在因而否一次性的读取全部的内容,并将每行的信息做为列表中的一个子项。java

例如:文件test.txt中python

1,3,4
2,35,6
分别用readline与readlines对其进行读取ios

r=file_object.readline();函数

结果为1,3,4

r=file_object.readlines();ui

结果为['1,3,4n', '2,35,6']

三、文件迭代spa

使用迭代器的file.next()用于读取文件的下一行。相比for循环,更复杂,通常采用 for循环直接迭代。操作系统

四、文件移动指针

seek(off,whence=0)能够在文件中移动文件指针到不一样的位置,,从文件中移动off个操做标记(文件指针),正往结束方向移动,负往开始方向移动。若是设定了whence参数,就以whence设定的起始位为准,0表明从头开始,1表明当前位置,2表明文件最末尾位置。
tell()能够展现 咱们的移动过程,展现咱们的当前位置htm

五、os模块

六、文件写入f.write();writelines()接受一个字符串列表做为参数
须要手动输入换行符n;

fobj=open('test','w');#直接在指定路径下打开test1 ,若是没有则直接生成,但若存在,则出错;
fobj.write('foon');
fobj.write('barn');
fobj.close();

结果为

foo

bar

import os;
file_object=open(r'E:Pythoniostream_testtest.txt','r+');
aline=raw_input("Enter a line ('.'to quit):");
if aline !=".":
file_object.write('%s%s' % (aline,os.linesep));

在文件test.txt中写入一条字符串结果为txt 文件中的一个内容

标准文件

通常程序一执行,就能够访问3个标准文件,分别是标准输入(通常是键盘)、标准输出(到显示器的缓冲输出)和标准错误(到屏幕的非缓冲输出),这里的缓冲、非缓冲是指open()的三个参数。

文件系统

对文件系统的访问大多经过python的os模块实现。该模块是python访问操做系统功能的主要接口。

os除了对进程和进程运行环境进行管理外,os模块还负责处理大部分的文件系统操做,包括删除/重命名文件,遍历目录树,已经管理文件访问权限等。

另外一个os.path 模块能够完成针对路径名的操做,它提供函数 完成管理和操做文件路径中的各个部分,获取文件或者子目录信息,文件路径查询等操做。

针对os path的操做,操做对象E:Pythoniostream_test文件及其下的test.txt文件

os.path.exists(),检测指定路径的文件或者目录是否存在。

import os;
for tempdir in ('/test.txt',r'E:Pythoniostream_testtest.txt'):
if os.path.exists(tempdir):
print 'yes';
break;
else:
print 'no temp directory available';
tempdir=' ';

结果为yes,

若in中改成('/test.txt',r'D:Pythoniostream_testtest.txt'),则结果为no temp directory available

os.path.isdir(),检测指定了路径是否存在且为一个目录,只能是目录,不然报错。

import os;
for tempdir in ('/test.txt',r'E:Pythoniostream_testtest.txt'):

in中检测的是文件,而非目录,因此未输出yes

if os.path.isdir(tempdir):
print 'yes';
break;
else:
print 'no temp directory available';
tempdir=' ';

输出no temp directory available

import os;
for tempdir in ('/test.txt',r'D:Pythoniostream_testtest.txt'):

指定路径在D盘,于是不存在

if os.path.isdir(tempdir):
print 'yes';
break;
else:
print 'no temp directory available';
tempdir=' ';
import os;
for tempdir in ('/test.txt',r'E:Pythoniostream_test'):
if os.path.isdir(tempdir):
print 'yes';
break;
else:
print 'no temp directory available';
tempdir=' ';

输出的是yes

同理可得os.path.isfile()只可检测指定路径是否存在且为一个文件

如下针对os中某些进行练习,针对文件的操做,因先检测是否存在指定路径,再对该路径或者路径中的文件作操做。更多的练习能够看read.md

import os;
for tempdir in ('/tmp',r'E:Pythoniostream_test'):
if os.path.isdir(tempdir):#检测指定路径是否存在且为一个目录,并赋给tempdir
print 'yes';
break;
else:
print 'no temp directory available';
tempdir=' ';

if tempdir:
os.chdir(tempdir); #改变当前工做路径
cwd=os.getcwd(); #获取当前工做路径;
print 'current temporany directory is :';
print cwd;
print os.listdir(cwd);

print 'creating example directory';
os.mkdir('example'); #在当前目录下新建一个新的文件
os.chdir('example'); #改变目录到example的文件下
cwd=os.getcwd();#获取example的文件路径
print 'new working directory:'
print cwd;
print ' original directory listing :'
print os.listdir(cwd);#列出(example)指定路径下的文件
os.chdir(tempdir);
cwd=os.getcwd();
print os.listdir(cwd);#列出(tempdir)指定路径下的文件
结果为:

current temporany directory is :

E:Pythoniostream_test

['pspathex.py', 'read.md', 'read.py', 'test.txt']

creating example directory

new working directory:

E:Pythoniostream_testexample

original directory listing :

[]

['example', 'pspathex.py', 'read.md', 'read.py', 'test.txt']

os.path.join()方法将分离的各部分组合成一个路径名

path=os.path.join(cwd,os.listdir(cwd)[0]);
print ' full file pathname:'
print path;

结果为E:Pythoniostream_testexamplefiletest.txt

os.path.split(path)方法将组合路径分红(路径名,文件名)

path=os.path.join(cwd,os.listdir(cwd)[0]);
print os.path.split(path);#(pathname,basename)

结果为('E:Pythoniostream_testexample', 'filetest.txt')

os.path.splitext(os.path.basename(path))方法将文件分红(文件名,文件扩展名)

path=os.path.join(cwd,os.listdir(cwd)[0]);
print os.path.splitext(os.path.basename(path));#(filename,extension)

结果为('filetest', '.txt')

转载于https://www.javazhiyin.com/20...

相关文章
相关标签/搜索