文件系统和文件
非序列化数据是不能简单地存储到系统文件当中的.
linux文件系统的体系结构:
python 文件对象
python 文件对象这种文件可以使非目录以外的其他的非常规文件,如管道文件,套接文件等等,但不包括目录。因为目录是文件系统的组成部分,而不是文件内容的组成部分。而python文件系统模块是os模块。
python 打开文件
VAR_NAME=open(file_name[mode,[bufsize]])
mode: r w a r+ w+ a+
b表示以二进制模式打开文件:
rb wb ab rb+ wb+ ab+
缓冲:
0:禁用
负数:使用系统默认缓冲
1:使用缓冲,只缓冲一行数据
2+:指定缓冲空间大小
f=open()
next(f)
f.close()
在这里插入代码片
>>> f=open("G:/neo4j操作指南/neo4j.txt","r")
>>> f.fileno()
4
>>> f.readline()
'####neo4j数据库的介绍文档网址:\n'
>>> f.readlines()
[' https://neo4j.com/docs/cypher-manual/3.5/syntax/naming/\n', '\n', '\n', '1:neo4j数据库的概念
\n', '2:内容关系架构\n', '3:Cypher语句\n', '\n', '\n', '创建一个新的节点和关系\n', '###创建了3个节点
FRIEND关系对应为: adam->pernilla pernilla->david\n', "CREATE (adam:User { name: 'Adam' })
,(pernilla:User { name: 'Pernilla' }),(david:User { name: 'David'\n", ' }),(adam)-[:FRIEND]->(perni
lla),(pernilla)-[:FRIEND]->(david)\n', '\n', ' .....................................................
..\n', ' ####创建5个多属性的节点,并建立某些节点之间的某些关系\n', " create (A:User{name:'Alice',ey
es:'brown',age:'38'},),(B:User{name:'Bob',eyes:'blue',age:25}),(C:User{name:'Charlie'\n", " ,eyes:'g
reen',age:'53'}),(D:User{name:'Daniel',eyes:'brown'}),(E:User{name:'Eskil',arrray:['one','two','thr
ee'],eyes:'\n", " blue',age:'41'}),(A)-[:KNOWS]->(B),(A)-[:KNOWS]->(C),(C)-[:KNOWS]->(D),(B)-[:KNOWS
]->(D),(B)-[:MARRIED]->\n", ' (E)\n', ' \n', ' .....................................................
......\n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' ###look
for friends of friends of Adam 寻找Adam的朋友的朋友\n', "MATCH (user:User { name: 'Adam' })-[r1:FR
IEND]-()-[r2:FRIEND]-(friend_of_a_friend)\n", 'RETURN friend_of_a_friend.name AS fofName\n', '\n', '
###look for friends of friends of Adam 寻找Adam的朋友的朋友(包括自己)\n', "MATCH (user:User { nam
e: 'Adam' })-[r1:FRIEND]-(friend)\n", 'MATCH (friend)-[r2:FRIEND]-(friend_of_a_friend)\n', 'RETURN f
riend_of_a_friend.name AS fofName\n', '\n', '\n', '###look for friends of friends of Adam 寻找Adam
的朋友的朋友\n', "MATCH (user:User { name: 'Adam' })-[r1:FRIEND]-(friend),(friend)-[r2:FRIEND]-(frie
nd_of_a_friend)\n", 'RETURN friend_of_a_friend.name AS fofName\n', '\n', '\n', '############# ######
#######################################\n', '##########值和类型 value and type###################
###########\n', '\n', '\n', '\t\t\t\t\t\t\t\t\t1属性类型\t\t\t\t\t\tProperty types\n', '\t\t\t\t\t\t
\t\t\t2结构类型\t\t\t\t\t\tStructural types\n', '\t\t\t\t\t\t\t\t\t3复合类型\t\t\t\t\t\tComposite ty
pes\n', '\t\t\t\t\t\t\t\t\t\n', '\t\t\t\t\t\t\t\t\t\n', '\t\t\t\t\t\t\t\t\t\n', '\t\t\t\t\t\t\t\t\t
第一种:Property types\n', '\t\t\t\t\t\t\t\t\tProperty types 用在哪些方面:\n', '\t\t\t\t\t\t\t\t\t
? Can be returned from Cypher queries\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t
\t\t\t\t\t\t? Can be used as parameters\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t? Can be st
ored as properties\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t? Can be constructed with Cypher
literals\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n', '\t\t\t\t\t\t\t\t\tProperty types\t
类型组成:\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tNumber, an abstract type, which has the s
ubtypes Integer and Float 数\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tString
\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tBoolean \n', '\t\t\t\t\
t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tThe spatial type Point 空间类型
持续时间\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tTemporal ty
pes: Date, Time, LocalTime, DateTime, LocalDateTime and Duration 时间\n', '\t\t\t\t\t\t\t\t\t\t第
二种:Structural types\n', '\t\t\t\t\t\t\t\t\t\tStructural types使用注意:\n', '\t\t\t\t\t\t\t\t\t\t
? Can be returned from Cypher queries\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t
\t\t\t? Cannot be used as parameters\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t? Cannot be stor
ed as properties\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t? Cannot be constructed with Cypher
literals\n', '\t\t\t\t\t\t\t\t\t\tStructural types的组成:\n', '\t\t\t\t\t\t\t\t\t\t
Structural types comprise:\n', '\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tNodes,
comprising: 节点,组成\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tId\n', '\t\
t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tLabel(s)\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\
t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tMap (of properties)\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t
\t\t\tRelationships, comprising: 关系,组成\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\
t\t\t\tId\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tType\n', '\t\t\t\t\t\t\t\t\
t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tMap (of properties)\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t
\t\t\t\t\t\t\t\t\t\t\t\tId of the start and end nodes\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\
t\t\t\t\tPaths \n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tAn alternating se
quence of nodes and relationships #交替的节点序列和关系\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\
t\t\t\n', '\t\t\t\t\t\t\t\t\t\t\t注:Nodes, relationships, and paths are returned as a result of pat
tern matching.\n', '\t\t\t\t\t\t\t\t\t\t\t Labels are not values but are a form of pattern syn
tax\n', '\t\t\t\t\t\t\t\t\t\t\t \n', '\t\t\t\t\t\t\t\t\t\t\t \n', '\t\t\t\t\t\t\t\t\t\t
第三种:Composite types\n', '\t\t\t\t\t\t\t\t\t\tComposite types使用注意:\n', '\t\t\t\t\t\t\t\t\t\t
\t\t\t\t\t\t? Can be returned from Cypher queries\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t? Can be used
as parameters\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t? Cannot be stored as properties\n', '\t\t\t\t\t
\t\t\t\t\t\t\t\t\t\t\t? Can be constructed with Cypher literals\t\n', '\t\t\t\t\t\t\t\t\t\tLists are
heterogeneous, ordered collections of values, each of which has any property, structural or composi
te type.\n', '\t\t\t\t\t\t\t\t\t\tMaps are heterogeneous, unordered collections of (key, value) pair
s, where:\n', '\n', '\t\t\t\t\t\t\t\t\t\tthe key is a String\n', '\t\t\t\t\t\t\t\t\t\tthe value has
any property, structural or composite type\t\n', '\n', '####################### ####################
#########################\n', '##########Naming rules and recommendations命名规则和建议###########\n
', '\t\t\t\t\t\t\t\t\t命名规则:\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t1Must begin with an alphabetic
letter \n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t2.Can contain numbers, but not as the first character
\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t3.Cannot contain symbols. \n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t
\t\t4.Can be very long, up to 65535 (2^16 - 1) or 65534 characters, depending on the version of Neo4
j.\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t5.Are case-sensitive 大小写敏感\n', '\t\t\t\t\t\t\t\t\t\t\
t\t\t\t\t\t6.Whitespace characters: 空格字符\n', '\t\t\t\t\t\t\t\t\tScoping and namespace rules范
围和命名空间规则:\n', '\t\t\t\t\t\t\t\t\t Node labels, relationship types and proper
ty names may re-use names.\n', "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tThe following query?—?with a for
the label, type and property name?—?is valid: CREATE (a:a {a: 'a'})-[r:a]→(b:a {a: 'a'}).\n", '\t\
t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tVariables for nodes and relationships must not re-use names within th
e same query scope.\n', '\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tThe following query is not valid as
the node and relationship both have the name a: CREATE (a)-[a]→(b).\n', '\t\t\t\t\t\t\t\t\t\n', '#
#########################################################################\n', '#####################
##############expressions\t表达,表示######################\n', '\n', '\t\t\t\t\t\t\tSimple CASE for
m: comparing an expression against multiple values\n', '\t\t\t\t\t\t\tGeneric CASE form: allowing fo
r multiple conditionals to be expressed\n', '\t\t\t\t\t\t\tDistinguishing between when to use the si
mple and generic CASE forms\n', '\t\t\t\t\t\t\t\n', '\t\t\t\t\t\t\t\n', '\t\t\t\t\t\t\t2.3.3.1. Simp
le CASE form: comparing an expression against multiple values\n', '\t\t\t\t\t\t\t2.3.3.2. Generic CA
SE form: allowing for multiple conditionals to be expressed 允许表达多个条件\n', '\t\t\t\t\t\t\t2.
3.3.3. Distinguishing between when to use the simple and generic CASE forms\n', '\n', '\n', '\n', '\
n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n',
'\n', '\t\t\t\t\t\t\t\t\n', '\t\t\t\t\t\t\t\t\t\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n', '\t\t\t\t\t\
t\t\t\n', ' 知识图谱的整体概况:\n', ' 讲解方面:\n', ' 1.市场应用\n', ' 2,技术应用 \
n', ' \n', ' 知识图谱的概况:\n', ' 1.来源\n', ' 2.概念\n', ' 3.现状\n', ' 4.技术应用 \n',
' 5.知识图谱与知识库的区别\n', ' \n', ' \n', ' 发展历程:\n', ' \n', ' \n', ' \n', '
\n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', '
\n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', '
\n', ' \n', ' \n', ' \n', '\n', '\n', '\n', '\n', '\n', '\t\t\t\t
\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n', '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n', '\t\t\t\t
\t\t\n', '\n', '\n', '\n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' ']
f.close()
#tell()返回当前指针在文件中的位置
#### seek() 指定文件指针的位置
file.seek (offset[whence])
whence:起点
0:从文件头
1:从当前位置
2:从文件尾部
offset:偏移量
f.isatty() 判断文件是否为终端文件
truncate的使用
f.truncate()方法用于从文件的首行首字节开始截断,截断文件为 size 个字节,无 size 表示从当前位置截断;截断之后 位置size后面的所有字节被删除,其中 Widnows 系统下的换行代表2个字节大小 。返回的结果是int,即截取的起始位置。
truncate()使用注意如下图
>>> f=open("G:\\neo4j操作指南\\neo4j.txt","r+")
>>> f.readline()
'你是谁,为了谁。\n'
>>> f.truncate()
88
>>> f.readline()
'小胖喜欢笑\n'
>>> f.readline()
'喜欢唱歌\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
0\x00\x00'
>>> f.readline()
''
>>> f.readline()
''
>>> f.readline()
''
>>> f.readline()
''
>>> f.readline()
''
>>> f.readline()
''
>>> f.readline()
''
>>> f.readline()
''
>>> f.readline()
''
>>> f.readline()
''
>>> f.readlines()
[]
>>> f.close()
>>> f=open("G:\\neo4j操作指南\\neo4j.txt","r+")
>>> f.readline()
'你是谁,为了谁。\n'
>>> f.truncate(50)
50
>>> s=f.readline()
>>> print(s)
小胖喜欢笑
>>> print(s)
小胖喜欢笑
>>> s=f.readline()
>>> print(s)
喜欢唱歌
原始文件neo4j.txt内容:
“小胖喜欢裙子”
“小胖有点忧伤”
“小胖是个吃货”
“小胖很温柔”
“小胖喜欢芒果”
“小胖喜欢周杰伦”
文件的属性:
f.closed 判断文件时否为关闭模式,返回boolean 类型的值
f.name 显示文件的目录
f.encoding 显示文件的编码
f.mode 显示获取当前文件使用的读写(打开)模式
f.newlines 返回到当前位置之前的所有行结束符的字串
python2中的属性:
f.softspace
Python 文件 flush() 方法是用来把文件从内存buffer(缓冲区)中强制刷新到硬盘中,同时清空缓冲区。一般情况下,文件关闭后会自动刷新到硬盘中,但有时你需要在关闭前刷新到硬盘中,这时就可以使用 flush() 方法。
完结
本篇主要介绍了python文件对象。
下一篇主要介绍python文件系统模块 os模块。