Several solutions to the small problem of Python found in the forum __python

Source: Internet
Author: User
(Just as a collection, easy to use)
-----------------------------------------
Question: Python search for path-forward issues:

My current project has two catalogues.
base/
Among them are logger.py
tests/

of which tests/testlogger.py
Import base. Logger

Perform
#pythone tests/testlogger.py
I can't find the logger module.

If the tests/testlogger.py CP to the upper directory can be useful

****************************
Import Sys
Sys.path.append ('.. ')
Import base. Logger

-------------------------------------------------------------------------------

Question: How Python iterates through all the files in a folder and invokes the EXE file with parameters.
**************************************
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:
Call filename with parameter
**************************************
For Root, dirs, the 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 () function:
**************************************
The range () function can generate a arithmetic progression. For example, the result of a range (n) is a list of all integers in [m,n] with a list,range (m,n) result that contains all the integers in the 0~n-1, and Range (M,n,d) just represents the list that starts with M, and does not exceed the n-1 with D as the tolerance.
-------------------------------------------------------------------------------
Break and continue statements, and ELSE statements that are used with the Loop statement:
**************************************
The function of break and continue is the same as in C language.

It is noteworthy that the for and while statements can be paired with the else-guided statement. Obviously, it means that the operation is performed when the cyclic condition is no longer valid. The example in the document is a program that determines whether each integer in [2,10] is a prime number.

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 '


-------------------------------------------------------------------------------
How python can get a path to the imported module:

**************************************
>>> 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 '
>>>
-------------------------------------------------------------------------------
How to get the current application path in Python:

**************************************
Import OS
Print Os.path.abspath (Os.curdir)
Print Os.path.abspath ('. ')

**************************************
Os.chdir (path) # change Dir
OS.GETSWD () # display Dir

-------------------------------------------------------------------------------
PYTHON and C interact with each other to invoke instance resolution:

**************************************
Pre-use tools:

VC + + Compiler

Python Interpreter

If not installed VC, you can go to Microsoft's next C + + compiler, address is as follows:

Http://download.microsoft.com/download/3/9/b/39bac755-0a1e-4d0b-b72c-3a158b7444c4/VCToolkitSetup.exe



After loading, add the python include and libs in the environment variable to the following 2 macros

INCLUDE

Lib



1, c call python

#include <Python.h>

int main (int argc, char *argv[])

{

Py_initialize ();

Pyrun_simplestring ("From time import time,ctime/n")

' Print ' is ', CTime (Time ())/n ');

Py_finalize ();

return 0;

}

Compiling directly with the CL file name is



2, use C to generate a DLL, with Python call

C code: such as 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 this 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 Definition File: Foo.def

Exports

Initfoo

Compile Build Foo.dll

Cl-c foo.c;

Link Foo.obj/dll/def:foo.def/out:foo.dll

Called in Python:

Import Foo

Dir (foo)

...

You can see the result:

>>> Import foo

>>> dir (foo)

[' __doc__ ', ' __file__ ', ' __name__ ', ' Bar ']

>>>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.