Python Tutorial- File Input Output Introduction

What is a file path and how do I write a file path in Python?

When the program is running, variables are a good way to save data, but the data stored in variables, sequences, and objects are temporary and will be lost after the program ends. If you want the data to remain after the program ends, you need to save the data to a file in. Python provides built-in file objects and built-in modules for manipulating files and directories. With these technologies, data can be easily saved to files (such as text files).

Regarding files, it has two key attributes, namely "file name" and "path". The file name refers to the name set for each file, and the path is used to indicate the location of the file on the computer. For example, on my Windows 7 notebook, I have a file named projects.docx (the part after the period is called the "extension" of the file, which indicates the type of file), and its path is at D:\demo\exercise, also That is, the file is located in the exercise subfolder of the demo folder under the D drive.

The file name and path can be analysed. Project.docx is a Word document, and both demo and exercise refer to "folders" (also called directories). A folder can contain files and other folders, such as project.docx in the exercise folder, which in turn is in the demo folder.

Note that D:\ in the path refers to the "root folder", which contains all other folders. In Windows, the root folder is named D:\, also known as the D: drive. In OS X and Linux, the root folder is /. This tutorial uses a Windows-style root folder. If you are typing an example of an interactive environment on OS X or Linux, use / instead.

In addition, additional volumes, such as DVD drives or USB flash drives, appear differently on different operating systems. On Windows, they are represented as new, character-driven root drives. Such as D:\ or E:\. On OS X, they are represented as new folders under the / Volumes folder. On Linux, they are represented as new folders under the / mnt folder. Also note that while folder and file names are not case sensitive on Windows and OS X, they are case sensitive on Linux.

Backslashes on Windows and forward slashes on OS X and Linux for Python

On Windows, path writing uses a backslash "\" as a separator between folders. But on OS X and Linux, use forward slashes "/" as their path separator. If you want your program to run on all operating systems, you must handle both cases when writing Python scripts.

Fortunately, it is easy to do this with the os.path.join() function. If you pass to it a single file and a string of folder names on the path, os.path.join() returns a string of file paths with the correct path separator. Enter the following code in the interactive environment:

import os
os.path.join('demo', 'exercise')

The output is:


Because this program runs on Windows, os.path.join('demo', 'exercise') returns 'demo\\exercise' (note that there are two backslashes because each backslash requires escaped by another backslash character). If this function is called on OS X or Linux, the string will be 'demo/exercise'.

Not only that, if you need to create a file storage path with a file name, the os.path.join() function is also useful. For example, the following example adds names from a list of file names to the end of the folder name:

import os
myFiles = ['accounts.txt', 'details.csv', 'invite.docx']
for filename in myFiles:
    print(os.path.join('C:\\demo\\exercise', filename))

The output is:



More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression

More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises