Discover python os module tutorial, include the articles, news, trends, analysis and practical advice about python os module tutorial on alibabacloud.com
Using the urllib or urllib2 module that comes with Pyhton to Capture webpages may be a bit lazy. let's take a look at some new things today. let's take a look at the tutorial of using the lxml module and the Requests module to capture HTML pages in Python:
Web capture
The
import the module.
Observe the program output. The first string of SYS. Path is empty-it indicates that the current directory is also part of SYS. path, which correspondsPythonpathThe environment variables are the same.
This means you can directly import the modules in the current directory. Otherwise, you must put your modules in one of the directories listed in SYS. Path.
Note the directory in which the program runs. This directory is the current d
',Filename='Myapp.log', FileMode='W') Logger= Logging.getlogger (__name__) Logger.debug ("This is debug message") Logger.info ("This is info message") logger.warning ("This is warning message")Operation Result:Myapp.log is Debug Message is Info Message is warning messageOS ModuleUse of the OS moduleThis module is used for invoking system commands through the OS
Transferred from: http://zhpfbk.blog.51cto.com/4757027/1561926Includes common operating system features in Python's standard library OS moduleThe OS module introduces the following method: Import OSThe following are common usage methods for OS modules:1, OS.GETCWD get the current working directory, that is, the program
One, subprocess module#Import OS#os.system (' tasklist ') #类似cmd输入系统命令" "The purpose of subprocess is to start a new process and communicate with it. Only one class is defined in the Subprocess module: Popen. You can use Popen to create processes and make complex interactions with processes. " "ImportSubprocess#can replace Os.systemImport Time#stdout command corr
The OS library in Python provides an interface for using a variety of operating system features. is often used in a library, so this article is mainly to summarize the introduction of Python in the standard library OS common methods, the text gives a detailed sample code for everyone to refer to the study, the need for
The OS modules in the Python standard library contain common operating system features. This module is especially important if you want your program to be platform-agnostic. That is, it allows a program to be written without any changes or problems, and can be run under Linux and Windows.Some of the more useful parts of the O
, new_img_name) I+ = 1exceptException as Error:Print('something went wrong, go ahead!') Continue #Print the number of successful pictures Print('Total Pictures:%d, now the picture is the first few:%d'% (Img_num, i))2. OS Module1. File Rename os module rename () can complete renaming of files rename (nee
System-related Information module: Import sysSys.Argvis aListthat contains all the command-line arguments.Sys.STDOUT SYS.STDIN SYS.StdErrThe file object that represents the standard input output and the error output, respectively.Sys.Stdin.ReadLine() Read a line from standard inputSys.StdOut.Write ( "a" ) screen output a sys. (exit_code) exit procedure Span class= "PLN" > Sys. is a dictionarymodule. get running operating system environment Sys.li
The Ile object is created using the Open function, and the following table lists the functions commonly used by file objects:
Serial Number
Method and Description
1
File.close ()Close the file. The file can no longer read and write after closing.
2
File.flush ()Refreshes the file internal buffer, directly writes the internal buffer data immediately to the file, rather than passively waits for the output buffer to write.
3
Installing SetuotoolExecute the command Curl Https://bootstrap.pypa.io/ez_setup.py-o-| PythonMac OS comes with Python 2.7.6 downloaded from the official website 2.7.9 after installation, the terminal input Python automatically changes to 2.7.9 version, and comes with PipExecute pip Install ScrapyError perhaps your account does not has write access to this directo
The OS module is a core module that Python comes with to interact with operating system objects.1. Import module for help>>> Import OS>>> Help (OS)>>> dir (
/b.txt ') os.rmdir ('/tmp/xx ')Write a simple shell in Python#!/usr/bin/pythonImport OS, syscmd = Sys.stdin.readline ()While CMD:Os.system (CMD)cmd = Sys.stdin.readline ()Writing platform-independent programs with Os.pathOs.path.abspath ("1.txt") = = Os.path.join (OS.GETCWD (), "1.txt")Os.path.split (OS.GETCWD ()) is used to separate the directory part and the file name portion of a directory name.Os.path.j
Day04 -- Python module, day04python
I. Module Introduction
A module is a set of code that implements a function. For example, several. py files can form a set of code, that is, a module. Common modules include the OS
Install the mysql-python module in python. Install the mysql connection module in the following steps: OS: winxp; python: 3.2? 32bit; setuptools: 2.0? Address: pypi. python. orgpypisetu
protection type.
The four methods can also be divided into public and private methods. Private functions cannot be called by functions other than the class, and private methods cannot be called by external classes or functions.
5. python converts a common function to a static method using the "staticmethod ()" or "@ staticmethod" command. Static methods are equivalent to global functions.
6. the python con
Translation The Python tutorial#modules
6. Modules
If you exit from the Python interpreter and then re-enter, the previously defined names (functions and variables) are lost. Therefore, if you want to write a longer program, it is better to use a text editor to prepare the interpreter input, using the file as an alternative input. This is also known as creating a
Sys:this module provides access to some variables used or maintained by the interpreter and to functions that interact St Rongly with the interpreter. It's always available. See also: https://docs.python.org/3/library/sys.html
Os:this module provides a portable the by using operating system dependent functionality. If you just want to read or write a file "see" open() if you want to manipulate paths, see
:open (' Lock.log ', ' W '). Write (username) Nbsp;print (' lockedby-is it locked %s '%username) breakcontinue print (' Run Results Create a file and write the last user nameEntering the correct user password will enter the game running program. Running three times here is the exit loop condition. Didn't write anything else.Operating status after lockoutSmall descriptionOpen is used to create a file. Open (' Lock.log ', ' W '). Write (username)Open a file named (' lock.log suffix file '), W is
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.