Discover python os module tutorial, include the articles, news, trends, analysis and practical advice about python os module tutorial on alibabacloud.com
I. Overview of OS ModulesThe Python OS module contains common operating system features. For example, copying files, creating files and folders ...Second, common methods1, Os.listdir () returns all file and directory names under the specified directory.2, Os.remove () delete a file.3, Os.system () run the shell command
whether the specified path is an absolute path
Isdir (PATH)
Determine if the specified path exists and is a directory
Isfile (PATH)
Determines whether the specified path exists and is a file
Islink (PATH)
Determines whether the specified path exists and is a symbolic link
Ismount (PATH)
Determine if the specified path exists and is a mount point
Samefile (path1,PAHT2)
Determine if the two paths of pat
Os.listdir (dirname): Lists directories and files under DirName os.getcwd (): Get current working directory Os.curdir: Returns the current directory ('. ') Os.chdir (dirname): Change the working directory to Dirnameos.path.isdir (name): Determine if Name is a directory, name is not a directory and return Falseos.path.isfile (name): Determine if name is a file, does not exist name also returns Falseos.path.exists (name): Determine if there is a file or directory Nameos.path.getsize (name): Get th
(1256,15) #杀死进程Os.pardir #返回当前目录的你目录, equivalent to (..), there are many options below the commandOs.curdir #返回当前目录, equivalent to (.), there are many options below the commandOs.environ #输出系统环境变量Os.tmpfile ("/tmp/tong/tong1.txt") #判断文件是否是临时文件Os.path.split ("/tmp/tong/tong1.txt") #返回目录和文件名Os.path.isabs ("/tmp/tong/tong1.txt") #判断路径是否是绝对路径Os.path.isdir ("/tmp/tong/tong1.txt") #判断路径是否是目录Os.path.isfile ("/tmp/tong/tong1.txt") #判断路径是否是文件Os.path.islink ("/tmp/tong/tong1.txt") #判断路径是否是链接文件Os.path.ism
1. OS module: Provides an interface for invoking the operating systemOS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work os.chdir ("DirName") changes the current script working directory, which is equivalent to the shell Cdos.curdir return current directory: ('. ') os.pardir Gets the parent directory s
Os.path.dirname (path) to the directory where path is returned. In fact, the first element of Os.path.split (path) os.path.basename returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path) OS. path.exists (path) If path exists, returns true if path does not exist, returns Falseos.path.isabs (path) If path is an absolute path, return Trueos.path.isfile (PATH) Returns true i
OS ModuleA module that the Python interpreter interacts with the operating system. Used to manipulate directories and files.# get current working directory Print (OS.GETCWD ())# Change Directory os.chdir ('C:')print(OS.GETCWD ()) # C +Os.chdir (Os.curdir) # ' . ' Go to the current directory Os.chdir (os.pardir) # ': ' Go to the parent directory os.makedirs (
Import OS module is required when using OS modules OS.GETCWD () module functions Function: Gets the current working directory, that is, the directory path of the current Python script work "no parameter" How to use:os.getcwd () Format such as: a = OS.GETCWD () #!/usr/bin/en
Analysis of Python OS and sys modules, pythonossys Module
The official explanation of the OS and sys modules is as follows:
OS: This module provides a portable way of using operating system dependent functionality.
This
= ' r ') existing = f[' key ']f.close () print (existing)Since shelve does not record any modifications to the persisted object by default, we need to modify the default parameters at Shelve.open () or the object's modifications will not be saved.# set Writebackimport shelvef1 = Shelve.open (' shelve_file ') print (f1[' key ') f1[' key ' [' new_value '] = ' This is not here Before ' f1.close () F2 = Shelve.open (' Shelve_file ', writeback=true) print (f2[' key ') ' f2[' key ' [' new_value '] = '
File Operations in the OS module:OS module attributesLinesep is a string used to split rows in a file.A string used to separate the file path name.Pathsep is a string used to separate file paths.String name of the current working directory of curdirPardir (current working directory) parent directory string name1. Rename: OS. Rename (old, new)2. Delete:
represents the end of the file. tell()We can show our mobile process and show our current position.5. OS Module6. File write F. write() ; writelines() accept a string 列表 as a parameterYou need to enter a newline character manually \ n;fobj=open('test','w');#直接在指定路径下打开test1 ,如果没有则直接生成,但若存在,则出错;fobj.write('foo\n');fobj.write('bar\n');fobj.close();#结果为#foo#barimport os;file_object=open(r'E:\
This article mainly introduces the powerful renames method for the OS module in python. For more information, see
Renames, a powerful renames method for the OS module in python
There are many powerful modules in
represents the end of the file. tell()We can show our mobile process and show our current position.
5. OS Module
6. File write F. write() ; writelines() accept a string 列表 as a parameterYou need to enter a newline character manually \ n;
fobj=open('test','w');#直接在指定路径下打开test1 ,如果没有则直接生成,但若存在,则出错;fobj.write('foo\n');fobj.write('bar\n');fobj.close();#结果为#foo#bar
import o
We often deal with files and directories. For these operations, python provides an OS module that contains a lot of functions for operating files and directories. All functions can be viewed using help (OS) or dir (OS.
Common functions are as follows:
1. rename:
Renames, a powerful renames method for the OS module in python
Renames, a powerful renames method for the OS module in python
There are many powerful modules in python. Among them, we o
Summary of the use of the OS module and Python learning notes
Copy codeThe Code is as follows:#! /Usr/bin/env python#-*-Coding: UTF-8 -*-Import OSPrint "n welcome everyone to learn Python with me ";System = OS. name; # obtain the
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.