Python中的用open打开文件错误,FileNotFoundError: [Errno 2] No such file or directory:
学习python的文件处理,刚开始打开文件,代码如下
f = open('test_file.txt', 'r', encoding='UTF-8')
RES = f.readlines()
print('读取的内容%s' % RES)
f.close()
这里运行报错FileNotFoundError: [Errno 2] No such file or directory: 'test_file.txt'
意思是没有这个文件?
后来发现是因为多了后缀
修改代码:把文件名的后缀去掉了
f = open('test_file', 'r', encoding='UTF-8')
RES = f.readlines()
print('读取的内容%s' % RES)
f.close()
运行成功:结果如下
读取的内容['1.zyh\n', '2.1995\n', '3.gggg']
注意:要读取的文件与py当前编辑的文件在同一个文件夹下,就不用加路径,不是同一个文件夹就要在文件名前面加路径
f = open('D:/zyh_test/test_file', 'r', encoding='UTF-8')
RES = f.readlines()
print('读取的内容%s' % RES)
f.close()
Python中的用open打开文件错误,FileNotFoundError: [Errno 2] No such file or directory:的更多相关文章
- python文件操作的坑( FileNotFoundError: [Errno 2] No such file or directory...)
环境:Windows8.1, Python3.6 pycharm community 2017 c盘下有一个配置文件:setup with open('c:\\setup','r') as ...
- 关于python中的 “ FileNotFoundError: [Errno 2] No such file or directory: '……'问题 ”
今天在学python时,在模仿一个为图片加上图标并移动到指定文件夹的程序时遇到“FileNotFoundError: [Errno 2] No such file or directory: '152 ...
- 关于JPype报FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/jvm'错误的解决
部署到线上的项目正常运行一年,今天早上突然报FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/jvm'错误. JPyp ...
- 对于python中“FileNotFoundError: [Errno 2] No such file or directory”的解决办法
在我们使用vscode运行Python代码时遇到的情况 一.出现原因:这里是由于Vscode中,python里的路径是相对与工作目录来进行定位的.所以在多级目录情况下,若不设置绝对路径,往往找不到相应 ...
- python3 使用selenium +webdriver打开chrome失败,报错:FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'
提示chrome driver没有放置在正确的路径下 解决方法: 1.chromedriver与chrome各版本及下载地址 驱动的下载地址如下: http://chromedriver.storag ...
- VSCode python 遇到的问题:vscode can't open file '<unprintable file name>': [Errno 2] No such file or directory
代码很简单,就两行: import pandas as pd import netCDF4 as nc dataset = nc.Dataset('20150101.nc') 环境:在VSCode中左 ...
- python中reload(module)的用法,以及错误提示
1.Python2中可以和Python3中关于reload()用法的区别. Python2 中可以直接使用reload(module)重载模块. Pyhton3中需要使用如下两种方式: 方式(1) ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
- Python 中当前位置以及目录文件遍历操作
Python 中当前位置以及目录文件遍历操作 当前位置 print(os.path.dirname(__file__)) 其中 dirname 会选择目录(文件夹),"__file__&qu ...
随机推荐
- python多层数组合成一个数组后循环打印出数组内的每一项元素的方法
a=[1,2,3,4] b=[5,6,7,8] c=[a,b] def test(c): for i in c: return i print(test(c)) 以上代码执行后打印出来的结果是 预期目 ...
- Mac系统 python2.7中安装MySQLdb
由于要在python2.7上使用到MySQLdb连接数据库,所以要安装MySQLdb,也就是MySQL-Python.安装之前已经有人告诉我,这个东西比较难装,果然我也遇到好多问题,在百度找了半天,发 ...
- Liunx(centos8)下的yum的基本用法和实例
yum 命令 Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的 ...
- zookeeper3.4.6安装
1.关闭防火墙 service iptables stop chkconfig iptables off 2.编辑hosts文件: vi /etc/hosts 192.168.99.6 JacK6 1 ...
- Web自动化测试项目(四)测试报告
测试报告生成 使用HTMLTestRunner 生成测试报告 本文使用的 HTMLTestRunner 来源于github: https://github.com/githublitao/HTMLTe ...
- 使用H5与webGL的3D 可视化地铁展示
前言 工业互联网,物联网,可视化等名词在我们现在信息化的大背景下已经是耳熟能详,日常生活的交通,出行,吃穿等可能都可以用信息化的方式来为我们表达,在传统的可视化监控领域,一般都是基于 Web SCAD ...
- 硬件原理系列之LED数码管(一)
LED数码管也叫数码显示器,由8段(7段,8多一位小数点)发光二极管组成,控制不同组合,就可以显示不同字符 dp示小数点,COM为公共端,根据连接方式的不同,分为共阴极和共阳极 工作原理:若选用共阴极 ...
- ConnectWeb
@echo off MODE con: COLS=60 lines=20 color a taskkill /f /im knatsvc.exe taskkill /f /im DrMain.exe ...
- C语言的puts(),gets(),putchar(),getchar()
其实puts(),gets()属于字符串输入函数. putchar()与getchar()属于字符输入函数. 1.字符函数 #include<stdio.h> int main(){ ch ...
- python学习记录(六)
0903--https://www.cnblogs.com/fnng/archive/2013/04/21/3034442.html 基本语句的用法 使用逗号输出(想要同时输出文本和变量值,又不希望使 ...