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

python – shutil.copyfile的替代品【代码】

我需要替代shutil模块,特别是shutil.copyfile. 这是一个鲜为人知的py2exe错误,使整个shutil模块无用.解决方法:嗯os.system("cp file1 file2") ?我不知道为什么shutil不能在py2exe中工作…你可能必须明确告诉py2exe包含该库…

如何在python select()中增加filedescriptor的范围【代码】

我测试python socket编程.并修改如下所示的选项在Mac中core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited file size (blocks, -f) unlimited max locked memory (kbytes, -l) unlimited max memory size (kbytes, -m) unlimited open files (-n) 100000 pipe size (512 bytes, -p) 1 stack size (kbytes, -s) 81...

在python中从textfile中读取多个变量的智能方法【代码】

我正在尝试加载存储在单个文本文件中的多个向量和矩阵(用于numpy).该文件如下所示:%VectorA 1 2 3 4 %MatrixA 1 2 3 4 5 6 %VectorB 3 4 5 6 7理想的解决方案是拥有一个字典对象,如:{'VectorB': [3, 4, 5, 6, 7], 'VectorA': [1, 2, 3, 4], 'MatrixA':[[1, 2, 3],[4, 5, 6]]}变量的顺序可以假定为固定的.因此,文本文件中出现顺序的numpy数组列表也可以.解决方法: from StringIO import StringIO mytext='''%VectorA 1 2 3 4 %Mat...

你如何在“python 2.7”中使用“os.path.isfile()”和一组“路径”但是一个变量文件名?【代码】

我正在研究一个计算器程序作为一个更大的项目的一部分,当我终于认为我完成它时,我测试了定义的“退出”命令.然而,它失败了,经过一些研究,我来到这里.我需要知道的是如何使(path)参数具有一组预定义的路径,但也有一个实际文件名的变量.例如:/ HDD / APPS /(此处插入变量). 这是错误和发生错误的行:File "../../C.py", line 19if ( not os.path.isfile('/HDD/APPS/'exe)):^SyntaxError: invalid syntax解决方法:连接两个字符串:>>...

无法从Python中读取tempfile【代码】

我试图在一个小python脚本中使用一些临时文件,并在使用tempfile-module时遇到问题: 测试代码import tempfile with tempfile.NamedTemporaryFile() as temp:print temp.nametemp.write('Some data')print temp.seek(0).read()产量s$python tmpFileTester.py /var/folders/cp/8kmr4fhs4l94y8fdc6g_84lw0000gn/T/tmpcDgrzF Traceback (most recent call last): File “tmpFileTester.py”, line 5, inprint temp.seek(0).read() A...

Pythonanywhere’staticfiles’不是有效的标记库:找不到模板库staticfiles【代码】

在pythonanywhere中,我正在使用带有Django 1.7和Python 2.7的virtualenv Settings.pySTATIC_ROOT = '/home/movies/pantherlist/movies/static/' STATIC_URL = '/static/'INSTALLED_APPS = ('django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','pantherlist.movies', )wsgi.pyactivate_this = '/home/movies/.virtual...

使用fileinput(Python)进行搜索和替换,同时还向控制台发送消息【代码】

我有线for line in fileinput.input(file_full_path, inplace=True):newline, count = re.subn(search_str, replace_str, line.rstrip())# ... display some messages to console ...print newline # this is sent to the file_full_path应该替换文件file_full_path中所有出现的search_str,并用replace_str替换它们. fileinput将stdout映射到给定文件.因此,打印换行符和发送到sys.stdout的内容将被发送到文件而不是控制台. 我想在进...

python – Kivy FileChooser:仅列出目录【代码】

如何仅列出Kivy FileChooser中的目录?我玩过滤器:filters: ['Directories, not files!']但文件再次列出.我关于回调过滤器,但没有找到示例. 我的Kv代码:<Saveto>:select: selectchooser: chooserorientation: 'vertical'FileChooserIconView:id: choosersize_hint_y: 0.9rootpath: homedirselect: Truefilters: ['How to list only folders?']Button:...select button...解决方法:这是一个例子:from kivy.app import App from k...

Python GTK3:如何创建Gtk.FileChooseDialog?【代码】

如何正确创建Gtk.FileChooseDialog? This popular tutorial说要使用如下代码:import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtkdialog = Gtk.FileChooserDialog("Please choose a folder", None,Gtk.FileChooserAction.SELECT_FOLDER,(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,"Select", Gtk.ResponseType.OK))但是,如果你使用python -W错误运行它(以捕获已弃用的警告),它说:File "test3.py", line...

python – Heroku上的django-import-export出现Tmpfile错误【代码】

我使用django-import-export来处理上传到我的Django管理站点的CSV文件. 当我在本地机器上运行Django时,一切正常. 当我将我的应用程序部署到Heroku时,我开始收到与tmpfile访问相关的错误:Feb 24 14:35:12 test-staging app/web.3: ERROR 2017-02-24 22:35:12,143 base 14 139687073408832 Internal Server Error: .... Feb 24 14:35:12 test-staging app/web.3: File "/app/.heroku/python/lib/python2.7/site-packages/impor...

Python: pyinstaller打包exe(含file version信息)【代码】【图】

最近项目上一直都是用Spyder直接运行.py文件的方式来执行每日的自动化程序,每天都要手动去点击Run来执行一次,所以考虑把.py文件直接打包成exe,然后用windows的task schedule功能来让它自动每天运行。 用pyinstaller来打包exe文件,同时为了便于维护,给exe文件添加文件版本file version信息。 1. 安装pyinstaller 机器上的python环境是直接安装的Anaconda软件,所以直接打开“Anaconda Prompt”来安装pyinstaller,运行命令: 在...

给出’int’对象的python cProfile是不可调用的错误【代码】

以下脚本运行很棒:$python myscript.py 当我尝试使用cProfile配置我的代码时:$python -m cProfile -s time myscript.py要么$python -m cProfile myscript.py我收到以下错误:Traceback (most recent call last):File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/runpy.py", line 121, in _run_module_as_main"__main__", fname, loader, pkg_name)File "/System/Library/Frameworks/Python.framew...

python – Django South没有为user_profiles创建表【代码】

我想重置我的Django项目的数据库,所以我做了以下步骤: >删除了我的SQLite数据库> python manage.py syncdb了吗?> python manage.py是否迁移了用户–fake 在我创建一个新帐户并登录后,我收到以下错误消息:no such table: users_userprofile这是我的用户model.py看起来像:class UserProfile(models.Model):user = models.OneToOneField(User)joined_goals = models.ManyToManyField(Goal, related_name="joined_goals")following...

python – 限制使用tkFileDialog.asksaveasfile时保存的文件扩展名【代码】

我目前正在使用Tkinter在python中编写GUI,用户可以在其目录中使用扩展名“.espace”保存GUI的状态. filename = tkFileDialog.asksaveasfilename(defaultextension =“.espace”) 如果用户指定了不同的文件扩展名,则文件将使用他们指定的扩展名保存.反正有没有阻止这个?这样他们完全被限制为使用扩展名“.espace”保存文件?解决方法:您可以使用’filetypes’选项指定文件类型.下面的示例将保存对话框中的文件类型下拉列表更改为.e...