Python中sys和os模块的区别

  1. sys: This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available. 参见:https://docs.python.org/3/library/sys.html
  2. os:This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see open(), if you want to manipulate paths, see the os.path module, and if you want to read all the lines in all the files on the command line see the fileinput module. For creating temporary files and directories see the tempfile module, and for high-level file and directory handling see the shutil module. 参见:https://docs.python.org/3/library/os.html

通俗地来说:

  1. sys用于利用Python编译器的一些变量或函数来完成任务。
  2. os用于利用操作系统的一些变量和函数(大多经过了重新命名以提高可移植性)来达到目的。

原文地址:https://www.cnblogs.com/lyg-blog/p/8988001.html

时间: 2024-08-29 02:56:27

Python中sys和os模块的区别的相关文章

python中sys和os的区别

<os和sys的官方解释> ?os os: This module provides a portable way of using operating system dependent functionality. 这个模块提供了一种方便的使用操作系统函数的方法. ?sys sys: This module provides access to some variables used or maintained by the interpreter and to functions that

python之sys与os模块详解

sys模块: sys.argv        命令行参数List,第一个元素是程序本身路径 sys.exit(n)      退出程序,正常退出时exit(0) sys.version      获取Python解释程序的版本信息 sys.maxint       最大的Int值 sys.path        返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值 sys.platform     返回操作系统平台名称 sys.stdin      输入相关 sys.stdout  

python中的堆排序peapq模块

heapq模块实现了python中的堆排序,并提供了有关方法.让用Python实现排序算法有了简单快捷的方式. heapq的官方文档和源码:8.4.heapq-Heap queue algorithm 下面通过举例的方式说明heapq的应用方法 实现堆排序 #! /usr/bin/evn python #coding:utf-8 from heapq import * def heapsort(iterable): h = [] for value in iterable: heappush(h

Python 中使用列表解析时候的区别

使用[] + for语句是解析列表 而使用() + for语句是产生生成器 实例代码如下: alist = [1, 2, 3, 4, 5] another_list = [i for i in alist] print another_list a_generator = (i for i in alist) print a_generator for i in a_generator: print i Python 中使用列表解析时候的区别

在python中扩展c语言模块

有一个以前写的c语言代码,我想把它用在python程序中.我先是看了<python基础教程>一书中的方法,书中说可以用swig加python内置distutils模块的方法来实现.我照着书上的步骤试了试,结果在导入模块的时候总是提示"ImportError: dynamic module does not define init function (initprintf)".起初我以为是so文件没有放对位置.但是我试着在目录中建立了一个简单的python模块,然后再导入,发

python中sys.setdefaultencoding(&#39;utf-8&#39;)的作用

在python中,编码解码其实是不同编码系统间的转换,默认情况下,转换目标是Unicode,即编码unicode→str,解码str→unicode,其中str指的是字节流,而str.decode是将字节流str按给定的解码方式解码,并转换成utf-8形式,u.encode是将unicode类按给定的编码方式转换成字节流str.注意调用encode方法的是unicode对象,生成的是字节流:调用decode方法的是str对象(字节流),生成的是unicode对象.若str对象调用encode会默

python中根据字符串导入模块module

python中根据字符串导入模块module 需要导入importlib,使用其中的import_module方法 import importlib modname = 'datetime' datetime_module = importlib.import_module(modname) print(datetime_module) # <module 'datetime' from 'C:\\Users\\huchengyue\\AppData\\Local\\Programs\\Pyth

Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即 str()或者 repr() . >>> a = 10 >>> type(str(a)) <class 'str'> >>> type(repr(a)) <class 'str'> 但是这二者之间有什么区别呢?因

python中time、datetime模块的使用

目录 python中time.datetime模块的使用 1.前言 2.time模块 1.时间格式转换图 2.常用方法 3.datetime模块 python中time.datetime模块的使用 1.前言 如果您从事过python web的开发,那一定有过这样的经历,对于各种复杂繁琐的业务逻辑,掺杂着各种各样的时间约束,让人很容易搞的头晕眼花,比如展示出一天内用户进行过的所有操作记录,再比如进行验证码验证时获取当前时间与验证码生成时间进行比较,检查是否超过10分钟过期时间之类.这些关于时间的业