免费行情软件网站下载大全东莞营销型网站

当前位置: 首页 > news >正文

免费行情软件网站下载大全,东莞营销型网站,广州市天气,wordpress多重分类文件的写入 和文件的读取一样#xff0c;文件的写入也有多种方法#xff0c;write#xff08;#xff09;和writelines#xff08;#xff09;方法。 二者之间的区别是#xff1a; write#xff08;#xff09;方法用于将字符串写入文件#xff0c;如果要写入文件的字… 文件的写入 和文件的读取一样文件的写入也有多种方法write和writelines方法。 二者之间的区别是 write方法用于将字符串写入文件如果要写入文件的字符串不多使用write方法即可 writelines用于将列表中存储的字符串写入到文件中用于将大量的字符串写入到文件中以提高效率。 例如 myfile (hello.txt, aw) temp [hello world!\n] myfile.writelines(temp)    #writelines的使用 strin hello! myfile.write(strin)              #write的使用 myfile.close() 文件的删除 文件的删除需要使用到os模块和os.path模块。os提供了对系统环境文件目录等操作系统级的接口函数。 文件的删除使用remove函数。 演示如下 myfile (myfile.txt, w) if os.path.exists(myfile.txt):     #判断文件是否存在注意后面的冒号 os.remove(myfile.txt)        文件的复制 file类中没有提供专门的文件复制函数因此只能通过使用文件的读写函数来实现文件的复制。这里仅仅给出范例 src file(myfile.txt, w) temp [hello world! \n] src.writelines(temp) src.close() src file(myfile.txt, r) des file(myfile2.txt, w) des.writelines(src.read()) src.close() des.close() shutil模块是另一个文件目录的管理接口提供了一些用于复制文件目录的函数。 copyfile函数可以实现文件的拷贝声明如下 copyfilesrc des 文件的剪切可以使用move函数模拟声明如下 movesrcdes 功能移动一个文件或者目录到指定的位置并且可以根据参数des重命名移动后的文件。 使用shutil来实现文件的拷贝 import shutil shutil.copyfile(myfile1.txt, myfile2.txt) shutil.move(myfile1.txt, ../)                 #把myfile1.txt移动到当前目录的父目录然后删除myfile1.txt shutil.move(myfile2.txt, myfile3.txt) #把myfile2.txt移动到当前目录并重命名myfile3.txt os模块的函数rename可以对文件或者目录进行重命名。 下面演示文件重命名操作。 如果当前目录存在名为myfile.txt的文件则重命名为myfile_rename.txt. import os li os.listdir(.)                                   #返回当前目录的文件列表 print li                                               #打印出当前目录包含的文件 if myfile.txt in li: os.rename(myfile.txt, myfile_rename.txt) 上面例子中是修改文件名但是文件还是统一类型 文件的后缀名没变有的时候需要将一种类型的文件改成另一种类型的文件这是就得利用rename和字符串查找的函数。 示例如下将后缀名为“html”格式的文件改成“htm”格式的文件 import os files os.listdir(.) for filename in files: pos filename.find(.) if filename[pos1:] html : newname filename[:pos1] htm os.rename(filename, newname) 以上过程还可以通过splitext来实现splitext用于将文件名和后缀名分隔开。 import os files os.listdir(.) for  filename in files : li  os.path.splitext(filename)      #返回文件名和后缀名组成的列表 if li[1] html: newname li[0] htm os.rename(filename, newname) 路径的匹配可以使用glob模块返回符合给定匹配条件的文件列表。例如上面的例子需要判断文件后缀是否是“html”类型可以使用glob直接进行匹配glob.glob“.html” 文件内容的查找和替换主要通过演示来说明其实现方法 【1】文件内容的查找从hello.txt中查找字符串“hello” 并统计“hello”出现的次数。 import re myfile file(hello.txt, r) count 0 for s in myfile.readlines:          #每次从hello.txt中读取一行保存到s中 li re.findall(hello, s)   #调用findall查询s 并将查询到的结果保存到li中 if len[li] 0:              #如果列表元素大于0则表示查询到字符串“hello”              count   count li.count(hello) print 查找到 str(count) 个hello   #调用count统计当前列表中“hello“出现的次数 myfile.close() 【2】文件内容的替换把hello.txt中的hello全部换为”hi“并把结果保存到myhello.txt中。 f1 file(hello.txt, r) f2 file(myhello.txt, w) for s in f1.readlines       f2.writes(s.replace(hello,hi))            #调用replace函数将s中 的“hello”替换为“hi”                                                                          #并把结果写入myhello.txt中 f1.close() f2.close() python中的文件操作和C语言里面的文件操作思想相同都是分为三步即打开文件读写文件最后要关闭文件只不过使用的函数不一样罢了。下面就稍微详细点的进行说明 (1)创建并且打开文件使用file()或者open()函数如果要打开的文件存在则打开否则创建该文件。 (2)调用read(),write()函数对文件进行读写。 (3)和C语言一样调用close()函数进行关闭文件。 举例如下 # -- coding: UTF-8 -*- myfile file(hello.txt, w) myfile.write(奥巴马 美国总统) myfile.close() myfile file(hello.txt,r) myfile read() print myfile myfile.close() 文件的读函数: 文件的读函数主要分为readline() readlines()read()函数。 readline()每次读取文件的一行因此需要使用永真式来判断是否已经读到文件结尾。 举例如下 myfile file(hello.txt, r)   #打开文件 while true:                             #读文件          line myfile.readline()          if line:                  print line          else:                  break myfile.close()                         #关闭文件 readlines()的使用和readline()差不多 只不过readlines()一次性读取多行并且也许要通过循环返回列表中的元素。 举例如下 myfile file(hello.txt, r) lines myfile.readlines()   # readlines()的返回值为列表。 if line in lines:                       #逐行读取列表的值        print line myfile.close() read()函数是从文件中一次性读取所有内容并赋给一个字符串变量。 举例如下 myfile file(hello.txt, r) lines myfile.read() print  lines myfile.close() 在开发过程中通常要对字典进行排序复制等操作和列表一样字典的排序也采用sorted函数字典的复制除了可以使用update函数外还可以使用copy函数但是得注意二者之间的区别。 字典的排序 先来演示字典的排序函数sort的使用 dict {a:apple,b:banana,g:grape,c:oreage} print dict #按照key进行排序。items可以用于字典的遍历返回keyvalue的序列lampda用于创建匿名函数并返回计算结果d[0]表示key print sorted(dict.items(), keylampda d:d[0]) #按照value进行排序。同样d[1]表示value print sorted(dict.items(), keylampda d:d[1]) 字典的复制 字典的复制前面用的是update函数这个方法是将字典A中的数据复制到字典B中且字典中原有的数据保持不变从而实现了字典B中数据的扩充。但是sorted不同将字典A中的数据复制到字典B中update会清除掉字典B中原有的数据。另外copy函数实现的是字典的浅拷贝deepcopy函数用于实现深拷贝。 下面演示copy函数的使用。 dict {a:apple,o:orange} dict2 {g:grape,b:banana} dict2 dict.copy() print dict2 dict2输出{a:apple,o:orange} 浅拷贝只是复制数据数据的引用并没有被复制因此新的数据和旧的数据使用同一块内存块 深拷贝则不一样它拷贝对象内部所有数据和引用相当于C语言中指针的作用。例如字典B浅拷贝字典A中的数据如果字典B中的数据发生修改字典A中的数据也将发生变化但是如果字典B深拷贝字典A中的数据则即使B中的数据变了A中也不会变。 下面演示深拷贝和浅拷贝 import copy dict {a:apple,o:orange} dict2 {b:banana,p:pear} #copy.deepcopy等价于dict.deepcopy dict2 copy.deepcopy(dict) #copy.copy 等价于dict.copy dict3 copy.copy(dict) dict2[a]watermelon print dict dict3[a]watermelon print dict