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