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

python – Boto3:使用upload_file()验证文件是否已上传【代码】

我正在使用boto3制作备份脚本,使用此行上传项目:bucket.upload_file(backup_file_name,bucket_path+backup_file_name)如何验证此文件是否已真正上传到我的存储桶?我试图用boto3 doc找到一些东西,但我没找到任何东西.此方法是否会抛出我可以处理的任何异常?解决方法:它会引发boto3.exceptions.S3UploadFailedError.您还可以执行head_object请求以验证对象是否应该如此.如果对象不存在,这将引发带有代码404的botocore.ClientError...

python – 摆脱easy_install消息:: module references __file__【代码】

模块名称:模块引用__file__ 当我使用easy_install安装我自己的软件包并且初始谷歌搜索没有带来任何成功时,这会出现几次. 我完全清楚我在模块中使用__file__,但它没有任何问题. 如何在不删除__file__引用的情况下删除此消息?解决方法:在谷歌搜索同样的问题时找到此页面.解决方案是: 告诉distutils你的包不是zip_safe(这没关系),如下所示:setup (name = 'yourmodule',...zip_safe = False,...)这样easy_install就不必解析你的模块...

Python File对象是真实文件吗?【代码】

在Python中,文件对象可能指向真实文件,stdin,stdout,stderr,甚至是其他东西.因此它可以是文件类型,也可以是file.flush中所述的真实文件. 如何知道文件是否是真实文件?我找到了一些猜测的方法,但似乎没有一个真正可靠: >文件不在[sys.stdin,sys.stdout,sys.stderr]中 – 似乎最安全,但仅适用于那些标准类型,不是非常通用的解决方案.> os.path.isfile(file.name) – 看起来非常安全,但如果在某些创建模式下打开新文件我可能无效.> ...

在python请求中使用curl语句`–form input = @./ thefile.pdf`【代码】

我从这个link中得到如下卷曲声明: curl -v -include –form input = @./ thefile.pdf localhost:8080 / processFulltextDocument 我正在尝试使用Requests在Python中复制上述语句,因此使用以下代码import requestsData = {'input': './samp.pdf'}url='http://127.0.0.1:8080/processFulltextDocument'r = requests.post(url,data=Data)print r.text但是,我收到415错误.我究竟做错了什么? 编辑curl语句的标题如下: curl -v -incl...

使用FileHandler的mode =’w’参数时,记录到python文件不会覆盖文件【代码】

我有一些代码在Python 2.7中设置日志(使用日志记录模块):import os import logging logger=logging.getLogger('CopasiTools') logger.setLevel(logging.DEBUG) log_filename=os.path.join(os.path.dirname(copasi_file),os.path.split(copasi_file)[1][:-4]+'_log.log') handler=logging.FileHandler(log_filename,mode='w') formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") handler.set...

python – 在自定义命名空间中调用execfile()在’__builtin__’命名空间中执行代码【代码】

当我调用execfile而不传递globals或locals参数时,它会在当前命名空间中创建对象,但是如果我调用execfile并为globals(和/或locals)指定一个dict,它会在__builtin__命名空间中创建对象. 请看以下示例:# exec.py def myfunc():print 'myfunc created in %s namespace' % __name__exec.py是从main.py execfile’d,如下所示.# main.py print 'execfile in global namespace:' execfile('exec.py') myfunc() print print 'execfile i...

python – 在线程内进行cProfile配置文件调用?

我在一些代码上运行了cprofile,其中包括执行大部分工作的几个线程.当我查看分析的输出时,我看到没有记录在线程内调用的所有函数.我确信他们被称为,因为他们做的事情很容易看到,如写入数据库等. cProfile不会分析线程吗?我错过了什么吗?解决方法:在这里找到答案:linkIt’s worth pointing out that using the profiler only works (by default) on the main thread, and you won’t get any information from other threads if y...

Python cProfile的严重开销?【代码】

嗨专家Python教徒,我开始使用cProfile,以便在我的程序上有更详细的计时信息.然而,令我非常不安的是,这是一个巨大的开销.知道为什么cProfile报告7秒,而时间模块只在下面的代码中报告2秒?# a simple functiondef f(a, b):c = a+b# a simple loop def loop():for i in xrange(10000000):f(1,2)# timing using time module # 2 seconds on my computer from time import time x = time() loop() y = time() print 'Time taken %.3f s....

【文件】ota_from_target_files的python脚本

源码如下(文末有分析):import?sys?? ?? if?sys.hexversion?<?0x02040000:?? ??print?>>?sys.stderr,?"Python?2.4?or?newer?is?required."?? ??sys.exit(1)?? ?? import?copy?? import?errno?? import?os?? import?re?? import?sha?? import?subprocess?? import?tempfile?? import?time?? import?zipfile?? ?? import?common?? import?edify_generator?? ?? OPTIONS?=?common.OPTIONS?? OPTIONS.package_key?=?"build/target/pr...

23、python基础学习-lesson_file_2【代码】

1 将文件输出到另一个文件2 # f_read = open("小重山","r",encoding="utf-8")3 # f_write = open("小重山2","w",encoding="utf-8")4 # number = 05 # for i in f_read :6 # number += 17 # if number == 5 :8 # i = "".join([i.strip(),"hello world\n"])9 # f_write.write(i) 10 # f_read.close() 11 # f_write.close() 12 # 13 # 昨夜寒蛩不住鸣。 14 # 惊回千里梦,已三更。 15 # 起来独自绕阶行。 16 # ...

python – Fabric命名空间想要一个fabfile【代码】

我正在完成Fabric Namespaces的基础教程. 我希望做类似于Structuring a fabric project with namespaces的事情 我的__init__.py文件看起来:from fabric.api import task@task def abc():pass当我运行fab –list时,我收到此错误:me@galvatron:/tmp/fabric_test$fab --listFatal error: Couldn't find any fabfiles!Remember that -f can be used to specify fabfile path, and use -h for help.Aborting.谁能告诉我我错过了什么或...

python – 在没有Window的情况下绘制和写入imagefile【代码】

我发现使用matplotlib只是将图表绘制到文件中并不像阅读教程那样容易.在教程中解释说,您只是累积数据,然后:import matplotlib.pyplot as plt # ... fill variables with meaningful stuff plt.plot(data.x,data.y,format_string) plt.savefig(filename)并做了.如果你只是将它作为shell执行,那也可以正常工作.但是,如果您将此代码提供给没有任何窗口的进程(如jenkins),那么您只会收到以下错误:Traceback (most recent call last):...

在python中绘制shapefile

我有几个形状文件,我想在其上绘制一些散点图数据. 有没有人有办法加载形状文件,然后绘制它?我已经按照了几个教程,但到目前为止还没有成功. 我试图使用的形状文件是巴基斯坦的道路之一.发现Here 我下载了模块pyshp和shapelib但是对其他人开放了!解决方法:有关shapefile的有用教程和使用底图绘制地图:http://www.packtpub.com/article/plotting-geographical-data-using-basemap

python – 不断在PyCharm中获取“Project Files Changed”消息,即使我没有更改项目中的任何文件

几乎就是它在标题中所说的 – 我不断在PyCharm中获得“Project Files Changed”消息.确切的信息是:Project components were changed externally and cannot be reloaded: RunManager Would you like to reload project?例如,Alt-Tabbing退出PyCharm几秒钟,然后Alt-Tabbing重新进入可能会生成消息.除非我切换到另一个应用程序,否则它通常不会发生,尽管有时它确实会发生. 有点难过.以前有人见过这个吗? 任何输入将不胜感激!解决方...

python – 我怎样才能避免:“ZipFile实例在提取zip文件时没有属性’__exit__””?【代码】

代码是:import sys execfile('test.py')在test.py我有:import zipfile with zipfile.ZipFile('test.jar', 'r') as z:z.extractall("C:\testfolder")此代码生成:AttributeError ( ZipFile instance has no attribute '__exit__' ) # edited从python idle运行时,“test.py”中的代码有效.我正在运行python v2.7.10解决方法:嗨也许迟到但我只是为我解决这个错误:). 我在python 2.7上创建我的代码但是当我把它放在使用2.6的服务器上...