Manipulating Files and directories

Source: Internet
Author: User

Manipulating Files and directories

1, if you want to manipulate files, directories, you can enter the command line under the operating system provides a variety of commands to complete, for example, DIR, CP and other commands

What if you want to perform these directories and files in a python program? In fact, the command provided by the operating system simply invokes the interface functions provided by the operating system, andPython's built-in OS module can also directly invoke the interface functions provided by the operating system

>>>import OS

>>>os.name

>>> ' POSIX ' if it is POSIX, the system is Linux, Unix or Mac OS X; if NT, Windows system

2, Environment variables: to get the value of an environment variable, you can call the Os.getenv () function

>>>import OS

>>>os.getenv (' path ')

3, Python in the file, folder operations often used in the OS module and Shutil module common methods

A, get the current working directory, that is, the current Python script work directory path: OS.GETCWD (); Os.path.abspath ('. ') view the absolute directory of the current directory

B. Returns all files and directory names under the specified directory: os.remove (' c:\\users\\zxq\\desktop\\total ') required to write \ \

C, function to delete a file: Os.remove (' C:\\users\\zxq\\desktop\\total\\test.txt ')

D, verify that the given path is a file: Os.path.isfile ('c:\\users\\zxq\\desktop\\total\\ test. txt') returns True, False

F, verify whether the given path is a directory: Os.path.isdiir ()

I, verify whether the given path is really present: os.path.exists ()

J

  

Os.chdir (' C:\Users\zxq\Desktop ') switch to the specified directory

OS.GETCWD () View current directory

Manipulating Files and directories

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.