在论坛中找到的几个关于Python的小问题的解决方法
来源:互联网 发布:react java 编辑:程序博客网 时间:2024/06/04 07:21
(只是作为收集,方便使用)
-----------------------------------------
问题 : python搜索路进的问题:
我现在的工程有两个目录
base/
其中有Logger.py
tests/
其中tests/testlogger.py
import base.Logger
执行
#pythone tests/testlogger.py
说找不到Logger模块
如果把tests/testlogger.py cp到上层目录就好用
****************************
import sys
sys.path.append('..')
import base.Logger
-------------------------------------------------------------------------------
问题: Python如何遍历一个文件夹下的所有文件,并且带参数调用其中的exe文件?
**************************************
import os, re
def enum_exe(arg, dirname, names):
for name in names:
if re.search(r'/.exe$', name.lower()):
dosomthing(dirname + "//" + name)
os.path.walk(".", enum_exe, ())
**************************************
import glob
fileList=glob.glob("E://PythonWork/*.exe")
for filename in fileList:
带参数调用filename
**************************************
for root, dirs, files in os.walk(path, topdown=False):
#hanlde file
for name in files:
if name[:-3] = 'exe':
print name
for subfolder in dirs:
print subfolder
-------------------------------------------------------------------------------
range()函数:
**************************************
range()函数可以生成一个等差数列。例如,range(n)的结果就是一个包含0~n-1中的所有整数的List,range(m,n)结果是在[m,n)中的所有整数的List,而range(m,n,d)刚表示从m开始,到不超过n-1的以d为公差的List。
-------------------------------------------------------------------------------
break和continue语句,以及在和循环语句一起使用的else语句:
**************************************
break和continue的作用和C语言中的一样。
值得注意的是,for和while语句后可以和else引导的语句搭配。显然,它的意思是说在循环条件不再成立时用执行的操作。文档中的例子是一个判断[2,10)中每个整数是否是质数的程序。
for n in range(2,10):
for x in range(2,n):
if n%x==0:
print n,'equals',x,'*',n/x
break
else:
print n,'is a prime number'
-------------------------------------------------------------------------------
python如何能得到一个被导入模块的路径:
**************************************
>>> import wxPython
>>> import imp
>>> imp.find_module("wxPython")
(None, 'C://Python25//lib//site-packages//wx-2.6-msw-unicode//wxPython', ('', '', 5))
>>>
**************************************
>>> import telnetlib
>>> telnetlib.__file__
'E://Python25//lib//telnetlib.py'
>>>
-------------------------------------------------------------------------------
python 中如何获取当前应用的路径:
**************************************
import os
print os.path.abspath(os.curdir)
print os.path.abspath('.')
**************************************
os.chdir(path) # change dir
os.getswd() # display dir
-------------------------------------------------------------------------------
PYTHON 与C相互交互调用实例解析:
**************************************
使用前工具:
Vc++编译器
Python 解释器
如没有装VC,可以去微软网站下一个C++的编译器,地址如下:
http://download.microsoft.com/download/3/9/b/39bac755-0a1e-4d0b-b72c-3a158b7444c4/VCToolkitSetup.exe
装完后,在环境变量中把PYTHON的INCLUDE和LIBS分别加入下面2个宏
INCLUDE
LIB
1、C中调用PYTHON
#include <Python.h>
int main(int argc, char *argv[])
{
Py_Initialize();
PyRun_SimpleString("from time import time,ctime/n"
"print 'Today is',ctime(time())/n");
Py_Finalize();
return 0;
}
直接用CL 文件名 编译就是
2、用C生成DLL,用PYTHON调用
C代码:如FOO.C
#include <Python.h>
/* Define the method table. */
static PyObject *foo_bar(PyObject *self, PyObject *args);
static PyMethodDef FooMethods[] = {
{"bar", foo_bar, METH_VARARGS},
{NULL, NULL}
};
/* Here's the initialization function. We don't need to do anything
for our own needs, but Python needs that method table. */
void initfoo()
{
(void) Py_InitModule("foo", FooMethods);
}
/* Finally, let's do something ... involved ... as an example function. */
static PyObject *foo_bar(PyObject *self, PyObject *args)
{
char *string;
int len;
if (!PyArg_ParseTuple(args, "s", &string))
return NULL;
len = strlen(string);
return Py_BuildValue("i", len);
}
C定义文件:foo.def
EXPORTS
initfoo
编译生成foo.dll
Cl -c foo.c;
link foo.obj /dll /def:foo.def /OUT:foo.dll
在PYTHON中调用:
Import foo
Dir(foo)
…
即可以看到结果:
>>> import foo
>>> dir(foo)
['__doc__', '__file__', '__name__', 'bar']
>>>
-----------------------------------------
问题 : python搜索路进的问题:
我现在的工程有两个目录
base/
其中有Logger.py
tests/
其中tests/testlogger.py
import base.Logger
执行
#pythone tests/testlogger.py
说找不到Logger模块
如果把tests/testlogger.py cp到上层目录就好用
****************************
import sys
sys.path.append('..')
import base.Logger
-------------------------------------------------------------------------------
问题: Python如何遍历一个文件夹下的所有文件,并且带参数调用其中的exe文件?
**************************************
import os, re
def enum_exe(arg, dirname, names):
for name in names:
if re.search(r'/.exe$', name.lower()):
dosomthing(dirname + "//" + name)
os.path.walk(".", enum_exe, ())
**************************************
import glob
fileList=glob.glob("E://PythonWork/*.exe")
for filename in fileList:
带参数调用filename
**************************************
for root, dirs, files in os.walk(path, topdown=False):
#hanlde file
for name in files:
if name[:-3] = 'exe':
print name
for subfolder in dirs:
print subfolder
-------------------------------------------------------------------------------
range()函数:
**************************************
range()函数可以生成一个等差数列。例如,range(n)的结果就是一个包含0~n-1中的所有整数的List,range(m,n)结果是在[m,n)中的所有整数的List,而range(m,n,d)刚表示从m开始,到不超过n-1的以d为公差的List。
-------------------------------------------------------------------------------
break和continue语句,以及在和循环语句一起使用的else语句:
**************************************
break和continue的作用和C语言中的一样。
值得注意的是,for和while语句后可以和else引导的语句搭配。显然,它的意思是说在循环条件不再成立时用执行的操作。文档中的例子是一个判断[2,10)中每个整数是否是质数的程序。
for n in range(2,10):
for x in range(2,n):
if n%x==0:
print n,'equals',x,'*',n/x
break
else:
print n,'is a prime number'
-------------------------------------------------------------------------------
python如何能得到一个被导入模块的路径:
**************************************
>>> import wxPython
>>> import imp
>>> imp.find_module("wxPython")
(None, 'C://Python25//lib//site-packages//wx-2.6-msw-unicode//wxPython', ('', '', 5))
>>>
**************************************
>>> import telnetlib
>>> telnetlib.__file__
'E://Python25//lib//telnetlib.py'
>>>
-------------------------------------------------------------------------------
python 中如何获取当前应用的路径:
**************************************
import os
print os.path.abspath(os.curdir)
print os.path.abspath('.')
**************************************
os.chdir(path) # change dir
os.getswd() # display dir
-------------------------------------------------------------------------------
PYTHON 与C相互交互调用实例解析:
**************************************
使用前工具:
Vc++编译器
Python 解释器
如没有装VC,可以去微软网站下一个C++的编译器,地址如下:
http://download.microsoft.com/download/3/9/b/39bac755-0a1e-4d0b-b72c-3a158b7444c4/VCToolkitSetup.exe
装完后,在环境变量中把PYTHON的INCLUDE和LIBS分别加入下面2个宏
INCLUDE
LIB
1、C中调用PYTHON
#include <Python.h>
int main(int argc, char *argv[])
{
Py_Initialize();
PyRun_SimpleString("from time import time,ctime/n"
"print 'Today is',ctime(time())/n");
Py_Finalize();
return 0;
}
直接用CL 文件名 编译就是
2、用C生成DLL,用PYTHON调用
C代码:如FOO.C
#include <Python.h>
/* Define the method table. */
static PyObject *foo_bar(PyObject *self, PyObject *args);
static PyMethodDef FooMethods[] = {
{"bar", foo_bar, METH_VARARGS},
{NULL, NULL}
};
/* Here's the initialization function. We don't need to do anything
for our own needs, but Python needs that method table. */
void initfoo()
{
(void) Py_InitModule("foo", FooMethods);
}
/* Finally, let's do something ... involved ... as an example function. */
static PyObject *foo_bar(PyObject *self, PyObject *args)
{
char *string;
int len;
if (!PyArg_ParseTuple(args, "s", &string))
return NULL;
len = strlen(string);
return Py_BuildValue("i", len);
}
C定义文件:foo.def
EXPORTS
initfoo
编译生成foo.dll
Cl -c foo.c;
link foo.obj /dll /def:foo.def /OUT:foo.dll
在PYTHON中调用:
Import foo
Dir(foo)
…
即可以看到结果:
>>> import foo
>>> dir(foo)
['__doc__', '__file__', '__name__', 'bar']
>>>
- 在论坛中找到的几个关于Python的小问题的解决方法
- 编译过程中碰到的几个小问题和解决方法
- 几个关于linux的论坛
- 在国外SEO论坛上讨论的几个小问题
- Python 编程中遇到的几个小问题
- 关于Java中String的几个小问题
- 在毕设过程中遇到的几个小问题
- 在c++中const引用引发的几个小问题
- 今天碰到的几个python 小问题
- 关于几个计算机系统的小问题
- 关于wince的几个小问题
- 关于索引使用的几个小问题
- 关于PHP的几个小问题
- 关于索引的几个小问题
- 关于Qt的几个小问题
- 关于NASM的几个小问题
- 链表中关于环的几个小问题
- 关于mysql的几个小问题
- [ASP.NET]如何给上传图像加版权信息
- Js 调用 WebService心得总结
- apt应用解析
- Spring学习笔记
- 公主的故事
- 在论坛中找到的几个关于Python的小问题的解决方法
- 关于hash在emule中的重要作用的文章
- UNIX下自动备份Oracle数据库
- 展望2007
- Oracle数据库系统性能优化策略
- SQL查询语句精华使用简要
- 面试常见37个问题
- 2007/02/28 星期三 晴
- Gentoo使用指南(ChinaUnix)