美文网首页
08文件读写和组织

08文件读写和组织

作者: Rachelyeah0926 | 来源:发表于2018-01-03 11:38 被阅读4次

1.python的文件读法在windows和Linux/os X上不一样,一个是backslash,一个是forward flash,为了统一,使用os.path.join()

import os

os.path.join(‘usr’,’bin’,’spam’)

2.修改文件路径;the current working directory

cwd文件夹

任何不是根目录里的文件都可以假设在pwd文件夹中,用os.getcwd()获取文件路径。同时也可以用os.chdir()更换文件路径;

import os
os.getcwd()
'C:\\Python34'
os.chdir('C:\\Windows\\System32')
os.getcwd()
'C:\\Windows\\System32'

3.创建文件

import os
os.makedirs('C:\\delicious\\walnut\\waffles')

4.相对路径绝对路径

#查看文件的绝对路径
os.path.abspath(path)
os.path.abspath('.\\Scripts')
'C:\\Python34\\Scripts'

#确定()内内容是否是绝对路径
os.path.isabs('.')
False
os.path.isabs(os.path.abspath('.'))
True

#查看一个文件对于另一个文件的相对路径
os.path.relpath(‘文件名’‘待比较文件名’)
>>> os.path.relpath('C:\\Windows', 'C:\\')
'Windows'
>>> os.path.relpath('C:\\Windows', 'C:\\spam\\eggs')
'..\\..\\Windows'
>>> os.getcwd() 'C:\\Python34'

5.基础名字和文件名

图片 1.png
path = 'C:\\Windows\\System32\\calc.exe'
os.path.basename(path)
'calc.exe'

os.path.dirname(path)
'C:\\Windows\\System32'

如果希望将文件的dirname, basename放在同一个字符串里:

>>> calc = 'C:\\Windows\\System32\\calc.exe'
>>> os.path.split(calc)
('C:\\Windows\\System32', 'calc.exe')

等同于

(os.path.dirname(calc), os.path.basename(calc))

所以split是个缩略词,所以比较好。

如果想将路径全部拆开,可以使用

>>>calc.split(os.path.sep)
['C:', 'Windows', 'System32', 'calc.exe']

如果是Linux和OS X的路径
则根目录会用空白表示,例如

>>>'/usr/bin'.split(os.path.sep)
['', 'usr', 'bin']

查看文件大小和文件目录

查看文件大小:os.path.getsize('/user/rachel/calc.exe')
查看文件目录(注意使用该函数是在os下,而不是os.path下):os.listdir('/user/user/rachel')

注意:getsize()只能获得一个文件的大小,如果想获得一个文件夹的大小,可以将os.path.getsize()和os.listdir()和os.path.join()一起用。

>>> totalSize = 0
>>> for filename in os.listdir('C:\\Windows\\System32'):
      totalSize = totalSize + os.path.getsize(os.path.join('C:\\Windows\\System32', filename))

>>> print(totalSize)
1117846456

1.验证路径是否正确,2.是否是一个文件,3,是否是文件夹

os.path.isdir()#是否为文件夹
os.path.isfile()#是否为文件
os.path.exists()#是否存在

文件的打开,阅读,书写

文件打开:

#如果是windows
>>> helloFile = open('C:\\Users\\your_home_folder\\hello.txt')

#如果是os X
>>> helloFile = open('/Users/your_home_folder/hello.txt')

文件阅读:

helloContent = helloFile.read()
print helloContent

文件阅读且保留file中跨行的格式:
例如hello.txt存在很多行
When, in disgrace with fortune and men's eyes,
I all alone beweep my outcast state,
And trouble deaf heaven with my bootless cries,
And look upon myself and curse my fate,

sonnetFile = open('sonnet29.txt')
sonnetFile.readlines()


[When, in disgrace with fortune and men's eyes,\n', ' I all alone beweep my
outcast state,\n', And trouble deaf heaven with my bootless cries,\n', And
look upon myself and curse my fate,']

相关文章

  • 08文件读写和组织

    1.python的文件读法在windows和Linux/os X上不一样,一个是backslash,一个是forw...

  • Python学习记录之:IO编程

    IO编程 文件读写 Python中文件读写语法和C兼容 读文件使用Python内置的open()函数,传入文件名和...

  • python文件读写

    1. 一般的读写文件方法 读取文件 写文件: 2. numpy读写文件 savetxt( ) 和 loadtxt(...

  • python学习笔记03

    文件处理 读写文件是最常见的IO操作。Python内置了读写文件的函数,用法和C是兼容的。 读写文件前,我们先必须...

  • 014.Python文件读写

    Python文件读写 1. 概述 读写文件是最常见的IO操作。Python内置了读写文件的函数,用法和C是兼容的。...

  • 读写文件1

    //读写分为顺序读写(从文件开始读写)和随机读写(比如定位文件指示器后读取第1000个字节位置的字符) //***...

  • C语言实战开发篇-9 文件结构

    文件读写 字符读写函数 :fgetc和fputc字符串读写函数:fgets和fputs数据块读写函数:fre...

  • Python 极简教程(二十五) - IO(文件读写)

    这里讲的文件读写,是由 Python 提供的最基础的文件读写,主要是对文本文件和二进制文件的读写。 如果想要操作 ...

  • C语言读写文件

    C语言文件读写### 标准文件读写 非标准文件读写 标准文件读写 头文件 include 打开文件 函数原型:FI...

  • python012-文件操作

    文件的基本操作 1 文件操作步骤 打开文件 读写等操作 关闭文件 注意:可以只打开和关闭文件,不进行任何读写操作。...

网友评论

      本文标题:08文件读写和组织

      本文链接:https://www.haomeiwen.com/subject/lvdcnxtx.html