Python small white (no programming foundation, no Computer Foundation) development of the road Auxiliary Knowledge 3 python OS usage

Source: Internet
Author: User

    • Get the path to the file
    • Import OS
    1. Os.path.dirname (__file__) Gets the path to the current file
    2. Print (Os.path.dirname (Os.path.dirname (__file__))) Gets the parent path of the directory where the current file resides

    3. Print (Os.path.dirname (Os.path.dirname (Os.path.dirname (__file__))) Gets the ancestor path to the parent directory of the directory where the current file resides

1. Os.name ()--judging the platform that is now being applied, Windows returns ' NT '; Linux returns ' POSIX '

2. OS.GETCWD ()--Get the current work directory.

3. Os.listdir ()--Specify all files and directory names in all directories. Cases:

Listed in the form of a list, which does not distinguish between directories and files.

4. Os.remove ()--delete the specified file

5. Os.rmdir ()--delete the specified directory

6. Os.mkdir ()--Create a directory

Note: This can only be done by building a layer, to make it available for recursion:os.makedirs ()

7. Os.path.isfile ()--Determines whether the specified object is a file. is return true, otherwise false

8. Os.path.isdir ()--Determines whether the specified object is a directory. is true, otherwise false. Cases:

9. Os.path.exists ()--verifies that the specified object exists. is true, otherwise false. Example:

os.path.split ()--Returns the directory and file name of the path. Cases:

This is just the two parts of the front and back apart. is to find the last one '/'. See Example:

OS.GETCWD ()--Get the current working directory (get present work dir)

Os.system ()-Executes the shell command. Cases:

Note : When you run the shell command here, if you want to invoke a variable before python, you can do so in the following way:

Var=123os. environ [' var ']=str (Var)//Note here [] is "string" Os.system (' echo $var ')

Os.chdir ()--Change directory to the specified directory

os.path.getsize ()--Gets the size of the file, if it is a directory, returns 0

Os.path.abspath ()--Get the absolute path. Cases:

os.path.join (path, name)--Connection directory and file name. Cases:

17.os.path.basename (path)--Return file name

os.path.dirname (path)--Return file path

19. Get the actual directory where the program is located

Import Osimport sysif __name__ = = "__main__":    print Os.path.realpath (sys.argv[0])    print os.path.split ( Os.path.realpath (sys.argv[0])    print os.path.split (Os.path.realpath (sys.argv[0])) [0]

Execution results

123 /home/jihite/ftp/del.py(‘/home/jihite/ftp‘‘del.py‘)/home/jihite/ftp 
    • Os.path.split () separate directories and file areas
12345 >>> import os>>> os.path.split("a/b/c/d")(‘a/b/c‘‘d‘)>>> os.path.split("a/b/c/d/")(‘a/b/c/d‘‘‘)

*********************

The command to execute is equivalent to the command entered in the Windows CMD window.

If it's a Linux system, it's the Linux Terminal Input command.

#coding =utf-8
From selenium import Webdriver
Import OS
Driver = Webdriver. Firefox ()
File_path = ' file:///' + os.path.abspath (' checkbox.html ')
Driver.get (File_path)

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

    • System () is provided in the Python OS module to execute the Systems command.

Like we're going to execute the e:\\test_object\\ directory.
The all_test.py file below can be implemented as follows:

#coding =utf-8
Import OS
Os.system (' e:\\test_object\\all_test.py ')

Or

Import OS
Os.chdir ("E:\\test_object")
Os.system (' Python all_test.py ')

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

    • All files under the directory can be obtained by Os.listdir ()
    • Through Os.path.getmtime (path) #返回在此path下最后一次修改的时间
    • Connecting directories and filenames via os.path.join (path, name)

#coding =utf-8
Import OS
#定义文件目录
Result_dir = ' E:\\test_object\\report '
Lists=os.listdir (Result_dir)
#重新按时间对目录下的文件进行排列
Lists.sort (Key=lambda fn:os.path.getmtime (result_dir+ "\ \" +FN))
Print (' latest file: ' +lists[-1])
File = Os.path.join (Result_dir,lists[-1])
Print file

Python small white (no programming foundation, no Computer Foundation) development of the road Auxiliary Knowledge 3 python OS usage

Related Article

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.