在论坛中找到的几个关于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']

>>>
原创粉丝点击