iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python内置函数4-execfile
  • 182
分享到

python内置函数4-execfile

函数pythonexecfile 2023-01-31 02:01:19 182人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

Help on built-in function execfile in module __builtin__:execfile(...)    execfile(filename[, globals[, locals]])       

Help on built-in function execfile in module __builtin__:


execfile(...)

    execfile(filename[, globals[, locals]])

    

    Read and execute a python script from a file.

    The globals and locals are dictionaries, defaulting to the current

    globals and locals.  If only globals is given, locals defaults to it.

execfile(filename[, globals[, locals]])

This function is similar to the exec statement, but parses a file instead of a string. It is different from the import statement in that it does not use the module administration — it reads the file unconditionally and does not create a new module. [1]


The arguments are a file name and two optional dictionaries. The file is parsed and evaluated as a sequence of Python statements (similarly to a module) using the globals and locals dictionaries as global and local namespace. If provided, locals can be any mapping object. Remember that at module level, globals and locals are the same dictionary. If two separate objects are passed as globals and locals, the code will be executed as if it were embedded in a class definition.


Changed in version 2.4: fORMerly locals was required to be a dictionary.


If the locals dictionary is omitted it defaults to the globals dictionary. If both dictionaries are omitted, the expression is executed in the environment where execfile() is called. The return value is None.


Note The default locals act as described for function locals() below: modifications to the default locals dictionary should not be attempted. Pass an explicit locals dictionary if you need to see effects of the code on locals after function execfile() returns. execfile() cannot be used reliably to modify a function’s locals.


中文说明:

可以调用文件来执行,当如果执行文件需要参数时就将参数放在sys.argv中即可


#execfile.py 

import sys 

if __name__ == '__main__': 

        print sys.argv 

        print 'execfile'

        sys.argv = 'appcfg.py update sdblog'.split(); 

        print sys.argv 

        execfile('main.py')

#main.py 

import sys 

def main(): 

        print 'main'

        print sys.argv 

if __name__ == '__main__': 

        main()

执行execfile.py test结果如下:

D:\GAE\dev>execfile.py test 

['D:\\GAE\\dev\\execfile.py', 'test'] 

execfile

['appcfg.py', 'update', 'sdblog'] 

main 

['appcfg.py', 'update', 'sdblog']


--结束END--

本文标题: python内置函数4-execfile

本文链接: https://www.lsjlt.com/news/186543.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python内置函数4-execfile
    Help on built-in function execfile in module __builtin__:execfile(...)    execfile(filename[, globals[, locals]])       ...
    99+
    2023-01-31
    函数 python execfile
  • 4 Python的函数
    概述         在上一节,我们介绍了Python的数据类型,包括:数字、字符串、列表、元组、集合、字典等内容。在本节中,我们将介绍Python的函数。在Python中,函数是一段可以重复使用的代码块,它可以提高代码的可重用性和可维护性...
    99+
    2023-08-31
    python 函数 lambda 函数的定义 函数的调用 参数的传递
  • python内置函数
    什么是内置函数 就是python给你提供的,拿来直接用的函数, 比如print 和 input等等. 截止到python版本3.6.2 python一共提供了68个内置函数. 他们就是python直接提供给我们的,有一些我们已经见过了. ...
    99+
    2023-01-30
    函数 python
  • python 内置函数
    python内置了一系列的常用函数,以便于我们使用python。基本的数据操作基本都是一些数学运算(当然除了加减乘除)、逻辑操作、集合操作、基本IO操作,然后就是对于语言自身的反射操作,还有就是字符串操作。官方文档:https://docs...
    99+
    2023-01-30
    函数 python
  • python内置函数1
    1.r=compile(s,"<string>","exec")  compile()将字符串编译成python代码2.exec(r)  执行python代码3.eval("8*6") eval("")里面只能执行表达式,执行e...
    99+
    2023-01-31
    函数 python
  • Python的内置函数
    1.什么是内置函数   就是python给你提供的. 拿来直接⽤的函数, 比如print., input等等. 截止到python版本3.6 python一共提供了68个内置函数. 他们就是python直接提供给我们的 Makedo...
    99+
    2023-01-31
    函数 Python
  • Python之内置函数
    ''' 内置函数 :     作用域相关(2) :         locals : 返回当前局部作用域内的所有内容         globals : 返回全局作用域内的所有内容     基础数据类型相关(38) :         和数...
    99+
    2023-01-31
    函数 Python
  • Python系列-python内置函数
    本文转载自:http://www.javaxxz.com/thread-359303-1-1.htmlabs(x)返回数字的绝对值,参数可以是整数、也可以是浮点数。如果是复数,则返回它的大小all(iterable)对参数中的所有元素进行迭...
    99+
    2023-01-31
    函数 系列 Python
  • 第4课 python input()函数
    上次,我们已经总结过基本的内容,今日我们说input() 其实也 已经说过了。。。。检讨完,还是该干嘛的要干嘛,写完日志再走。刚刚好我们也遇到最少的内容。。。。写几行吧。。。服务很重要。。。。 input() 是输入字符串的,python...
    99+
    2023-01-31
    函数 python input
  • Python内置函数详谈
    这种图皆取自python.org,列出了python3.10中的内置函数。 但是,这些真的都是函数吗? 我们来测试一下: import types import inspect...
    99+
    2024-04-02
  • python内置函数2-bytearra
    Help on class bytearray in module __builtin__:class bytearray(object) |  bytearray(iterable_of_ints) -> bytearray. | ...
    99+
    2023-01-31
    函数 python bytearra
  • python内置函数3-dir()
    Help on built-in function dir in module __builtin__:dir(...)    dir([object]) -> list of strings        If called wit...
    99+
    2023-01-31
    函数 python dir
  • python内置函数3-complex(
    Help on class complex in module __builtin__:class complex(object) |  complex(real[, imag]) -> complex number |   |  C...
    99+
    2023-01-31
    函数 python complex
  • python 之 python3内置函数
    一. 简介   python内置了一系列的常用函数,以便于我们使用,python英文官方文档详细说明:点击查看, 为了方便查看,将内置函数的总结记录下来。   二. 使用说明...
    99+
    2024-04-02
  • python内置函数3-compile(
    Help on built-in function compile in module __builtin__:compile(...)    compile(source, filename, mode[, flags[, dont_in...
    99+
    2023-01-31
    函数 python compile
  • python-常用内置函数
    1.其他   - len / open / id / range / type 2.输入输出   - print / input  3.强制转换   - dict / list / tuple / int / str / bool / ...
    99+
    2023-01-31
    函数 常用 python
  • python内置函数3-delattr(
    Help on built-in function delattr in module __builtin__:delattr(...)    delattr(object, name)        Delete a named attr...
    99+
    2023-01-31
    函数 python delattr
  • zero python.3 内置函数
    内置函数...
    99+
    2023-01-31
    函数 python
  • python内置函数map/reduce
    python有几个内置的函数很有意 思:map/filter/reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是python列表方法的三架马车。 filte...
    99+
    2023-01-31
    函数 python reduce
  • python内置函数3-cmp()
    Help on built-in function cmp in module __builtin__:cmp(...)    cmp(x, y) -> integer        Return negative if x<y...
    99+
    2023-01-31
    函数 python cmp
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作