【Python:os.isfile()和os.listdir()之间的分歧】教程文章相关的互联网学习教程文章

python – 从csv-file读取数据并转换为正确的数据类型【代码】

我有以下问题.我写了一个二维列表,其中每列具有不同的类型(bool,str,int,list),到csv文件.现在我想再次从csv文件中读出数据.但我读到的每个单元都被解释为一个字符串. 如何自动将读入数据转换为正确的类型?或者更好:是否有可能告诉csv-reader每列的正确数据类型? 示例数据(如csv文件中):IsActive,Type,Price,States True,Cellphone,34,"[1, 2]" ,FlatTv,3.5,[2] False,Screen,100.23,"[5, 1]" True,Notebook, 50,[1]解决方法:作...

python3(三十五)file read write【代码】

""" 文件读写 """ __author__on__ = shaozhiqi 2019/9/23# !/usr/bin/env python3 # -*- coding: utf-8 -*-# 读文件的模式打开一个文件对象,使用Python内置的open()函数,传入文件名和标示符 f = open(D:/temp/shao.txt, r, encoding=UTF-8) print(f.read()) f.close() # 执行结果 # 1.ashahh # 2.343erer # 文件使用完毕后必须关闭,因为文件对象会占用操作系统的资源,并且操作系统同一时间能打开的文件数量也是有限的# ------...

python3(三十七) filepath【代码】

""" file path """ __author__on__ = shaozhiqi 2019/9/23# !/usr/bin/env python3 # -*- coding: utf-8 -*-# os模块的基本功能 import osprint(os.name) # nt # 如果是posix,说明系统是Linux、Unix或Mac OS X,如果是nt,就是Windows系统# ---------------------------------------------------------------------------- # 要获取详细的系统信息,可以调用uname()函数: # print(os.uname()) # 注意uname()函数在Windows上不提...

从zip中提取文件而不使用python ZipFile保留结构?【代码】

我尝试从包含一个文件夹中的子文件夹的.zip中提取所有文件.我希望子文件夹中的所有文件只在一个文件夹中提取,而不保留原始结构.此刻,我提取所有内容,将文件移动到文件夹,然后删除以前的子文件夹.具有相同名称的文件被覆盖. 在写文件之前可以这样做吗? 这是一个结构,例如:my_zip/file1.txt my_zip/dir1/file2.txt my_zip/dir1/dir2/file3.txt my_zip/dir3/file4.txt最后我告诉你:my_dir/file1.txt my_dir/file2.txt my_dir/file3...

python – FileNotFoundError:[Errno 2]没有这样的文件或目录【代码】

参见英文答案 > Python open() gives IOError: Errno 2 No such file or directory 3个我试图打开一个CSV文件但由于某种原因python无法找到它. 这是我的代码(它只是一个简单的代码,但我无法解决问题):import csvwith open('address.csv','r') as f:reader = csv.reader(f)for row in reader:print row解决方法:当您打开名为address.csv的文件时,您告诉open()函数您的文件位于当前工作目录中.这...

Python错误:io.UnsupportedOperation:fileno【代码】

我正在使用服务器和来自此链接的客户端程序:http://www.bogotobogo.com/python/python_network_programming_tcp_server_client_chat_server_chat_client_select.php 当我运行客户端时遇到以下错误:Traceback (most recent call last):File "client.py", line 26, in clientread_sockets, write_sockets, error_sockets = select.select(socket_list , [], []) io.UnsupportedOperation: fileno我使用的是Python 3,但我已经使...

如何在Selenium Webdriver Python 3中使用Chrome Profile【代码】

因此,每当我尝试使用我的Chrome设置(我在默认浏览器中使用的设置)时,都会添加options = webdriver.ChromeOptions() options.add_argument("user-data-dir=C:\Users\... (my webdriver path)") driver = webdriver.Chrome(executable_path="myPath", options=options)它显示了错误代码SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes n 16-17: truncated \UXXXXXXXX escape在我的bash中.我不知道这意味着什...

python – PyQt4和PyQt5中的QFileDialog字符串有区别吗?【代码】

我有一个代码块,使用Python3和PyQt5打开QFileDialog:from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFileDialog import sysclass MCVE(QWidget):def __init__(self):super().__init__()self.initialize()def initialize(self):self.setWindowTitle('MCVE')self.setGeometry(50, 50, 400, 200)btn = QPushButton('Example', self)btn.clicked.connect(self.clicked)self.show()def clicked(self):filename = Q...

python – 模块__file__属性是绝对的还是相对的?

我无法理解__file__.据我所知,__ file__返回加载模块的绝对路径. 我遇到问题:我有一个带有一个语句的abc.py print __file__,从/ d / projects / python运行abc.py返回abc.py.从/ d /运行返回projects / abc.py.有什么原因吗?解决方法:从documentation:__file__ is the pathname of the file from which the module was loaded, if it was loaded from a file. The __file__ attribute is not present for C modules that are st...

Python 3:ResourceWarning:unclosed文件<_io.TextIOWrapper name ='PATH_OF_FILE'【代码】

当我使用“python normalizer / setup.py test”在python中运行测试用例时“我得到以下例外ResourceWarning: unclosed file <_io.TextIOWrapper name='/Users/minisha/workspace/govTech/aiworkspace/skillset-normalization-engine/normalizer/lib/resources/jobkredskills.taxonomy' mode='r' encoding='utf-8'>在代码中,我正在阅读如下的大文件:def read_data_from_file(input_file):current_dir = os.path.realpath(os.path.j...

ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed【代码】

ReadTimeoutError: HTTPSConnectionPool(host=‘files.pythonhosted.org’, port=443): Read timed out. 1. ReadTimeoutError: HTTPSConnectionPool(host=‘files.pythonhosted.org’, port=443): Read timed out. guipc@deepnorth01:~/people_counting_api$ sudo pip3 install opencv-python==3.4.0.14 The directory '/home/guipc/.cache/pip/http' or its parent directory is not owned by the current user and the cache has...

python – 如何使用QFileDialog知道要保存的文件类型【代码】

关于pyQt4 我更喜欢在QFileDialog中对getSaveFilename使用静态方法,以便用户可以看到Windows / Mac本机对话框. 我的问题是,如果用户没有在保存文件名中键入文件扩展名(比如选择图像类型以保存文件),那么我没有办法检查文件的类型他们想保存为. 如何创建一个用过滤器保存文件的对话框,以及如何知道用户选择的过滤器? 例如:files_types = "GML (*.gml);;Pickle (*.pickle);;YAML (*.yml)" file = QtGui.QFileDialog.getSaveFileNam...

获取“IOError:[Errno 2]没有这样的文件或目录:’myoutfile.csv’”错误在Python中【代码】

我正在使用此行创建一个新文件(该文件不存在):with open(outfilename, 'rwb') as outfile:它得到了这个错误:IOError: [Errno 2] No such file or directory: 'myoutfile.csv'我正在尝试创建这个文件,我想如果我使用’w’,如果它不存在就会创建它.如果是权限,如何创建新文件夹并引用其路径?解决方法:传递给open()函数的打开模式只接受特定的几个字母组合.在你的情况下,’rwb’不是那些组合之一,Python可能假设你的意思是’rb’.尝...

python – 你可以在创建文件后从NamedTemporaryFile更改删除标志吗?【代码】

如何在创建此类文件后更改NamedTemporaryFile中的删除标志? 为什么?虽然大多数时候我不需要保留临时文件,但如果我从代码中检测到错误,我想保留它们以便能够分析它们.解决方法:对于现在搜索这个问题的任何人,至少对于Python 3,在实例化NamedTemporaryFile之后更改删除不会改变其初始行为,因为实际标志存储在名为_closer的对象(这是_TemporaryFileCloser实例)中.请参阅Python中的tempfile.py,因为official Python docs没有提及任何...

创建FileCookieJar时Python中的AttributeError(cookielib Py 2.6)【代码】

我正在尝试使用Python 2.6中的cookielib创建一个新的FileCookieJar.但是,当我这样做时,我收到以下错误:Traceback (most recent call last):File "C:\Users\Paul\getfile.py", line 7, in <module> cj.load(os.getcwd()+'\cookies.txt') File "C:\Python26\lib\cookielib.py", line 1763, in load self._really_load(f, filename, ignore_discard, ignore_expires) AttributeError: FileCookieJar instance has no attribute '_rea...