问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

关于Python用open()找不到文件的问题

发布网友 发布时间:2022-04-22 05:03

我来回答

2个回答

懂视网 时间:2022-04-18 22:39

python内置函数open()用于打开文件和创建文件对象

语法

open(name[,mode[,bufsize]])

name:文件名

mode:指定文件的打开模式

  r:只读

  w:写入

  a:附加

  r+,w+,a+同时支持输入输出操作

  rb,wb+以二进制方式打开

bufsize:定义输出缓存

  0表示无输出缓存

  1表示使用缓冲

  负数表示使用系统默认设置

  正数表示使用近似指定大小的缓冲

#以只读方式打开text.txt文件,赋值给f1变量
>>> f1 = open('test.txt','r')

#查看f1数据类型
>>> type(f1)
<class '_io.TextIOWrapper'>

#读取文件内容,以字符串形式返回
>>> f1.read()
'h1
h2
h3
h4
h5
h6'

#此时指针处于文件末尾,通过tell获取当前指针位置,通过seek重新指定指针位置
>>> f1.readline()
''
>>> f1.tell()

>>> f1.seek(0)

#单行读取
>>> f1.readline()
'h1
'

#读取余下所有行,以列表方式返回
>>> f1.readlines()
['h2
', 'h3
', 'h4
', 'h5
', 'h6']

#文件名
>>> f1.name
'test.txt'

#关闭文件
>>> f1.close()

#文件写入
f2 = open('test.txt','w+')
f2.write('hello')
f2.close()

#向文件追加内容
f3 = open('test.txt','a')
f3.write('hello')
f3.close()

#通过flush,将缓冲区内容写入文件
#write将字符串值写入文件
f3 = open('test.txt','w+')
for line in (i**2 for i in range(1,11)):
 f3.write(str(line)+'
')
f3.flush()
#f3.close()

#writelines将列表值写入文件
f3 = open('test.txt','w+')
lines = ['11','22','33','44']
f3.writelines(lines)
f3.seek(0)
print(f3.readlines())
f3.close()
#执行结果:['11223344']

>>> f3.closed
True
>>> f3.mode
'w+'
>>> f3.encoding
'cp936'
Help on TextIOWrapper object:class TextIOWrapper(_TextIOBase) | Character and line based layer over a BufferedIOBase object, buffer. | 
 | encoding gives the name of the encoding that the stream will be | decoded or encoded with. It defaults to locale.getpreferredencoding(False). | 
 | errors determines the strictness of encoding and decoding (see | help(codecs.Codec) or the documentation for codecs.register) and
 | defaults to "strict". | 
 | newline controls how line endings are handled. It can be None, '', | '
', '
', and '
'. It works as follows: | 
 | * On input, if newline is None, universal newlines mode is
 | enabled. Lines in the input can end in '
', '
', or '
', and
 | these are translated into '
' before being returned to the | caller. If it is '', universal newline mode is enabled, but line | endings are returned to the caller untranslated. If it has any of | the other legal values, input lines are only terminated by the given | string, and the line ending is returned to the caller untranslated. | 
 | * On output, if newline is None, any '
' characters written are | translated to the system default line separator, os.linesep. If | newline is '' or '
', no translation takes place. If newline is any | of the other legal values, any '
' characters written are translated | to the given string. | 
 | If line_buffering is True, a call to flush is implied when a call to | write contains a newline character. | 
 | Method resolution order: | TextIOWrapper | _TextIOBase | _IOBase | builtins.object | 
 | Methods defined here: | 
 | getstate(...) | 
 | init(self, /, *args, **kwargs) | Initialize self. See help(type(self)) for accurate signature. | 
 | new(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | 
 | next(self, /) | Implement next(self). | 
 | repr(self, /) | Return repr(self). | 
 | close(self, /) | Flush and close the IO object. | 
 | This method has no effect if the file is already closed. | 
 | detach(self, /) | Separate the underlying buffer from the TextIOBase and return it. | 
 | After the underlying buffer has been detached, the TextIO is in an | unusable state. | 
 | fileno(self, /) | Returns underlying file descriptor if one exists. | 
 | OSError is raised if the IO object does not use a file descriptor. | 
 | flush(self, /) | Flush write buffers, if applicable. | 
 | This is not implemented for read-only and non-blocking streams. | 
 | isatty(self, /) | Return whether this is an 'interactive' stream. | 
 | Return False if it can't be determined.
 | 
 | read(self, size=-1, /) | Read at most n characters from stream. | 
 | Read from underlying buffer until we have n characters or we hit EOF. | If n is negative or omitted, read until EOF. | 
 | readable(self, /) | Return whether object was opened for reading. | 
 | If False, read() will raise OSError. | 
 | readline(self, size=-1, /) | Read until newline or EOF. | 
 | Returns an empty string if EOF is hit immediately. | 
 | seek(self, cookie, whence=0, /) | Change stream position. | 
 | Change the stream position to the given byte offset. The offset is
 | interpreted relative to the position indicated by whence. Values | for whence are: | 
 | * 0 -- start of stream (the default); offset should be zero or positive | * 1 -- current stream position; offset may be negative | * 2 -- end of stream; offset is usually negative | 
 | Return the new absolute position. | 
 | seekable(self, /) | Return whether object supports random access. | 
 | If False, seek(), tell() and truncate() will raise OSError. | This method may need to do a test seek(). | 
 | tell(self, /) | Return current stream position. | 
 | truncate(self, pos=None, /) | Truncate file to size bytes. | 
 | File pointer is left unchanged. Size defaults to the current IO | position as reported by tell(). Returns the new size. | 
 | writable(self, /) | Return whether object was opened for writing. | 
 | If False, write() will raise OSError. | 
 | write(self, text, /) | Write string to stream. | Returns the number of characters written (which is always equal to | the length of the string). | 
 | ----------------------------------------------------------------------
 | Data descriptors defined here: | 
 | buffer | 
 | closed | 
 | encoding | Encoding of the text stream. | 
 | Subclasses should override. | 
 | errors | The error setting of the decoder or encoder. | 
 | Subclasses should override. | 
 | line_buffering | 
 | name | 
 | newlines | Line endings translated so far. | 
 | Only line endings translated during reading are considered. | 
 | Subclasses should override. | 
 | ----------------------------------------------------------------------
 | Methods inherited from _IOBase: | 
 | del(...) | 
 | enter(...) | 
 | exit(...) | 
 | iter(self, /) | Implement iter(self). | 
 | readlines(self, hint=-1, /) | Return a list of lines from the stream. | 
 | hint can be specified to control the number of lines read: no more | lines will be read if the total size (in bytes/characters) of all | lines so far exceeds hint. | 
 | writelines(self, lines, /) | 
 | ----------------------------------------------------------------------
 | Data descriptors inherited from _IOBase: | 
 | dict

*with

为了避免打开文件后忘记关闭,可以通过管理上下文,当with代码块执行完毕时,内部会自动关闭并释放文件资源。

with open("test.txt","a+") as f:
 f.write("hello world!")

热心网友 时间:2022-04-18 19:47

你看提示信息,显示的文件名和你输入的不同,你是不是输入了特殊符号或者乱码呀。
另外,建议使用/代替\,例如'E:/biopython/seqA.txt'
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
结核病是什么样的疾病? 曹丕17岁得了肺痨,明知自己命不长久,还要强争王位,是不是很自私呢?_百... 古代小说常出现的病名 急求一篇"生活小窍门"(500字)的作文 至今最有什么小妙招 健康的戒烟方法 笔记本电池锁死是什么原因引起的? 黑龙江债权转让合同纠纷该怎样取证 安徽债权转让合同纠纷应该怎么样取证 房产官司律师费多少 退伍士官证丢了怎么办? 为什么python总是显示找不到文件! 微信聊天记录异常 如何解决恢复微信聊天记录重复叠加的问题 python对文件写入,&nbsp;新建的文件找不到 安卓手机微信聊天记录顺序混乱 士官证丢了会有什么后果吗? 拼音怎样读(如:王字是用那几个拼音组成的,怎么读等) 怎么用idm下载.py已经搭建python,在python中打开.... 微信聊天记录错乱 王海江拼音字母怎么写 为什么文件存在python却显示不存在 王华龙的拼音怎么写 电脑微信聊天记录错乱怎么恢复? 使用微信的时候出现聊天记录混乱闪退要怎么解决呢? 王春雨拼音怎么说 王小港的拼音怎么拼 王小刚的拼音怎么拼 python在打开txt文件时总是显示找不到文件 如图? 微信异常导致聊天记录混乱怎么修复呢? 士官证丢失使用证明能否登机? python 为什么明明有这个文件路径,但有时候会找不到 steam热血无赖怎么修改金钱 士官证丢了怎么找回? python3读取XML文件时一直是找不到文件 python使用open找不到文件怎么解决 武警士官证丢失会有怎样的处罚 最近刚学python,Pycharm运行程序时,提示无法找到... 灶具的主要厂家 士官证掉了登报格式怎么写? 顺德美的厨房电器厂普工的工资怎么样? 热血无赖2.1十项修改器怎么用?详细点。 试管婴儿移植后可以吃梨吗? 军官证遗失如何补办 常见生物填料都有哪些 肾移植后可以吃梨吗 芜湖美的厨卫电器制造有限公司怎么样 污水处理池的生物填料是什么意思 玩热血无赖用了一次修改器后发现卡就把修改器删除... 军人结婚士官证丢失正在补办中,只有身份证,能领...