python写的读写文件的脚本

一边学习python,一边练习python。这是一个好的学习方法。

下面这个脚本是根据课后题写的,吸收了别人的一些经验。只是一个很简单的脚本,大牛勿笑。

 

  1. ################################  
  2. #  Author :   netcat           #  
  3. #    QQ   :   297020555        #  
  4. #   Date  :   2012-1-3         #  
  5. # Version :   1.0              #  
  6. # Function: to read&write file #  
  7. ################################  
  8.  
  9. import os  
  10. ls=os.linesep  
  11.  
  12. numlist=['1','2','3']  
  13. def getnum():  
  14.     input=raw_input('''  
  15. ---------------------  
  16.         1.read  
  17.         2.write  
  18.         3.quit  
  19. ---------------------  
  20. enter your select num:\n''')  
  21.     if input in numlist:  
  22.         return input  
  23.     else:  
  24.         print 'enter error,input again'  
  25.         return getnum()  
  26.  
  27. def getfile():  
  28.     file=raw_input('enter filename:\n')  
  29.     return file  
  30.  
  31. def comment():  
  32.     print 'enter "quit" to exit'  
  33.     lines=[]  
  34.     line=raw_input('>')  
  35.     while True:  
  36.         if line=='quit':  
  37.             return lines  
  38.         else:  
  39.             lines.append(line)  
  40.             line=raw_input('>')  
  41.  
  42. def write():  
  43.     if os.path.exists(file):  
  44.         f=open(file,'a')  
  45.         f.writelines(['%s%s'%(x,ls) for x in com])  
  46.         f.close()  
  47.         print 'append done'  
  48.     else:  
  49.         f=open(file,'w')  
  50.         f.writelines(['%s%s'%(x,ls) for x in com])  
  51.         f.close()  
  52.         print 'write done'  
  53.  
  54. def read():  
  55.     if os.path.exists(file):  
  56.         f=open(file,'r')  
  57.         for line in f:  
  58.             print line,  
  59.     else:  
  60.         print 'file is not exists'  
  61.  
  62.  
  63. while True:  
  64.     num=getnum()  
  65.     if num=='1':  
  66.         file=getfile()  
  67.         read()  
  68.     elif num=='2':  
  69.         file=getfile()  
  70.         com=comment()  
  71.         write()  
  72.     else:  
  73.         print 'exit'  
  74.         break  
### 回答1: 在 Python读写文件非常简单。 读取文件: ```python # 打开文件 f = open("example.txt", "r") # 读取文件内容 content = f.read() # 关闭文件 f.close() # 输出文件内容 print(content) ``` 文件: ```python # 打开文件 f = open("example.txt", "w") # 文件内容 f.write("Hello, world!") # 关闭文件 f.close() ``` 另外, 也可以使用 'with' 语句来简化文件读写过程,避免忘记关闭文件导致资源泄漏 读取文件: ```python with open("example.txt", "r") as f: content = f.read() print(content) ``` 文件: ```python with open("example.txt", "w") as f: f.write("Hello, world!") ``` ### 回答2: Python操作读写文件脚本非常简单,可以用一些内置函数和方法来实现。 首先,要读取文件内容,可以使用`open`函数打开文件,该函数接收文件路径和打开模式作为参数。例如,要读取名为`myfile.txt`的文件,可以使用以下代码: ``` file = open("myfile.txt", "r") content = file.read() file.close() ``` 上述代码使用了`open`函数来打开文件,通过设置模式为"r"来指定只读模式。然后,可以使用`read`方法来读取文件内容,并将其存储在`content`变量中。最后,使用`close`方法关闭文件。 接下来,如果要文件内容,可以使用`open`函数打开文件,并指定打开模式为入模式"w"。例如,要将文本入名为`myfile.txt`的文件,可以使用以下代码: ```python file = open("myfile.txt", "w") file.write("Hello, World!") file.close() ``` 上述代码使用了`open`函数来打开文件,然后使用模式"w"来指定入模式。接着,使用`write`方法将文本文件。最后,使用`close`方法关闭文件。 值得注意的是,使用`write`方法文件时,如果文件已经存在,会将原有内容清空并覆盖。如果想要在文件末尾添加内容而不是覆盖原有内容,可以使用追加模式"a"来打开文件。 以上就是使用Python操作读写文件的基本脚本。当然,还有其他更复杂的读写文件的方法,包括逐行读取、指定编码等,但这些基本操作足以满足大多数需求。 ### 回答3: Python提供了丰富的方法来操作读写文件。首先需要用open()函数来打开文件,可以指定文件名和打开方式(只读、入、追加、二进制读写等)。比如,我们可以用以下代码来打开一个文件并读取内容: ```python with open("example.txt", "r") as file: content = file.read() ``` 这段代码会打开名为example.txt的文件并将其内容读取到变量content中。读取文件时,常用的方法有read()、readline()和readlines()。其中,read()会读取整个文件内容,readline()会逐行读取文件内容,而readlines()则会将文件内容读取为一个列表。 如果要文件,可以使用open()函数的"w"或"a"参数来指定入方式。其中,"w"表示入模式,会覆盖原有文件内容,而"a"表示追加模式,会在原有文件内容的末尾添加新内容。例如: ```python with open("example.txt", "a") as file: file.write("Hello, World!") ``` 这段代码会在名为example.txt的文件末尾添加一行内容"Hello, World!"。除了write()方法,还可以使用writelines()方法来入多行内容。 另外,可以使用os模块来操作文件相关的功能,比如重命名文件、删除文件等。例如,使用os.rename()函数来重命名文件: ```python import os os.rename("old.txt", "new.txt") ``` 这段代码会将名为old.txt的文件重命名为new.txt。 总之,Python提供了简单且灵活的方法来操作读写文件,通过open()函数和相关方法,可以轻松实现对文件的读取和入操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值