Python's deep understanding

Source: Internet
Author: User
Tags python script

working with files and directories

Python 3 comes with a module called OS, which stands for "Operating system" (operating system). The OS module contains a lot of functions for getting (and modifying) information about local directories, file processes, environment variables, and so on. Python does its best to provide a unified API on all supported operating systems, so you can include platform-specific code as little as possible while guaranteeing that your program can run on any computer.

Current working directory

when you are just starting to learn python, you will spend a lot of time in the Py
The shell. Throughout this book, you will always see examples like the following:

Thon

1. Import a module in the examples directory
2. Calling a function of a module
3. Interpreting the output results
always have a current working directory

If you don't know the current working directory, the first step is likely to get a importerror. Why? Because Python will look for the sample module in the import search path, the lookup will fail because the examples directory is not included in the search path. You can solve this problem by using one of the following two methods:

1. Add the examples directory to the import search path
2. Switch the current working directory to the examples directory

Python is secretly remembering the current working directory property at all times.
Whether you're running your own in a Python Shell or at the command line
Python script, or running a Python CGI script on a Web server,
The current working directory always exists.

OS module provides two functions to handle the current working directory

>>> Import Os①

>>> print (OS.GETCWD ()) ②

C:\Python31

>>> os.chdir ('/users/pilgrim/diveintopython3/examples ') ③

>>> print (OS.GETCWD ()) ④

C:\Users\pilgrim\diveintopython3\examples

1. The OS is Python's own; You can import it any time, anywhere.
2. Use the OS.GETCWD () function to get the current working directory. When you run a graphical Python shell, the current working directory defaults to the directory where the Python shell executable file resides. On Windows, this directory depends on where you install Python; The default location is C:\Python31. If you run the Python Shell from the command line, the current working directory is the directory where you run Python3.
3. Use the Os.chdir () function to change the current working directory
4. When running the Os.chdir () function, I always use the Linux style path (forward slash, no drive letter) even on Windows. This is one of the areas where Python tries to hide operating system differences.

Python's deep understanding

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.