koalafool 发表于 2020-10-23 22:41:52

学习啦感谢

GLINN 发表于 2020-11-14 22:17:54

本帖最后由 GLINN 于 2020-11-14 22:19 编辑

收藏

wdyy636 发表于 2020-11-16 14:51:41

6666

孤独枫 发表于 2020-11-18 16:19:16

刚刚学到这里

超速无奈 发表于 2020-11-27 19:29:36

学习了。

swufezxf 发表于 2020-12-2 15:24:22

1111111111111

zmgthy 发表于 2020-12-3 21:41:02

谢谢

bobowei 发表于 2020-12-7 09:39:39

先点赞再回头看文章

苏苏Shu 发表于 2020-12-8 18:11:18

甲鱼的语文是真的差!

SKY126 发表于 2021-1-5 19:05:10

3file_name= input("请输入要打开的文件:")
line = input("请输入需要显示的行数:")
file = open(file_name)

i = 0
for each in line:
    if each != ":" and each != ":":
      i += 1
    else:
      break

start = line[:i]
final = line


if i == 0:
    if len(line) == 1:
      print('文件%s的全文内容如下:' % file_name)
      print(file.read())
    elif len(line) > 1:
      print('文件%s从开始到第%s行内容如下:' % (file_name, final))
      count = 0
      for each_line in file:
            print(each_line)
            count += 1
            if count == float(int(final)):
                break
    else:
      pass
      
else:
    if final == "":
      print('文件%s从第%s行到末尾内容如下:' % (file_name, start))
      count = 1
      for each_line in file:
            if count >= int(start):
                print(each_line)
            count += 1
    else:
      print('文件%s从第%s行到第%s行内容如下:' % (file_name, start, final))
      count = 1
      for each_line in file:
            if int(final) >= count >= int(start):
                print(each_line)
            count += 1
      
      

file.close()

397226362 发表于 2021-2-9 10:41:30

我想问一下,一个任务那节课的文本去哪里下载呢{:10_257:}{:10_257:}

lingmumian 发表于 2021-2-16 22:13:51

小甲鱼视频考古过来的

weixiao0636 发表于 2021-2-20 12:41:07

file_name = input("请输入文件名:")
f = open(file_name + ".txt","w")

file_beginning = input("请输入内容,单独输入“:w”表示保存退出:")
file_content =[]

while 1:
    temp = input("")
    if temp!= ":w":
      file_content.append(temp)
      f.writelines(temp+"\n")
    else:
      break
   
f.close()

===============================================


f = open("E:\python作业包\Record.txt")
pline = len(f.readlines())-1
f.seek(0)

require = input("请输入需要显示的行数,此文件最高行数限制是"+str(pline)+":")
requirement = require.split(":",1)

if requirement :
    line_start = int(requirement)-1
else:
    line_start = 0

if requirement :
    line_end = int(requirement)-1
else:
    line_end = pline

print("文件从",line_start+1,"行到",line_end+1,"行的内容如下:")
now = line_start

while now <= line_end:

    print(f.readlines(1))
    now += 1
f.close()

solardo 发表于 2021-3-2 11:42:05

请问这些属性可以组合使用吗?
比如'rb'以二进制只读打开? 这样表示对吗

lisson 发表于 2021-3-12 12:14:11

{:5_108:}

有点空闲 发表于 2021-3-19 22:30:25

为什么我open一个文件会有这个错误
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
    f.read(10)
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa7 in position 64: illegal multibyte sequence

ourmaple 发表于 2021-5-23 22:47:59

111

yfcz6095 发表于 2021-6-20 18:01:31


import os
os.chdir('D:\\Desktop\\Python练习小程序\\a')

wj = input('输入要打开的文件名:')
h = input('输入要显示的行数【格式如13:15或:21或12:】:')
print()

f = open(wj)
lst=[]
f.seek(0)

for each in f:
    lst.append(each)



(begin,end)=h.strip().split(':')
if begin =='':
    begin = '0'
   
if end == '':
   end = '-1'

b = int(begin)
e = int(end)

if b == 0 and e == -1:
    print('文件 %s 的全文内容如下:' % wj)
    f.seek(0)
    print(f.read())
elif b == 0 and e != -1:
    print('文件 %s 从开始到第 %d 行内容如下:' % (wj,e))
    for i in range(0,e):
      print(lst)
elif b != 0 and e == -1:
    print('文件 %s 从第 %d 行到结尾的内容如下' % (wj,b))
    for i in range(b-1,len(lst)):
      print(lst)
else:
    print('文件 %s 从第 %d 行到第 %d 行的内容如下:' % (wj,b,e))
    for i in range(b-1,e):
      print(lst)
f.close()

yfcz6095 发表于 2021-6-20 18:02:46

#定位好文件路径#
import os
os.chdir('D:\\Desktop\\Python练习小程序\\a')
#定义各个需要输入的变量#
wj = input('输入要打开的文件名:')
gjz = input('输入要替换的关键字:')
xz = input('输入新的单词或字符:')
print()
#打开文件并定位到起始位置,定义数量和临时列表#
f = open(wj)
lst=[]
f.seek(0)
sl = 0
#用count函数计算数量#
for each in f:
    sl = sl + each.count(gjz)
print('文件 %s 中共有%d个【%s】' %(wj,sl,gjz))
#用replace函数替换字符#   
print('你确定要把所有的 【%s】替换为【%s】吗?' %(gjz,xz))
shifou = input('Y/N:')
if shifou == 'y' or 'Y':
    f.seek(0)
    for each in f:
      new = each.replace(gjz,xz)
      lst.append(new)
f.close()
#将修改后的字符写入文件,覆盖原文件#
f = open(wj,'w')
f.writelines(lst)
f.close()

   

Multiple-x 发表于 2021-6-30 15:08:11

鱼C有你更精彩^_^
页: 6 7 8 9 10 11 12 13 14 15 [16] 17 18
查看完整版本: 文件的打开模式和文件对象方法