Reading and Writing File in python
Reading and writing files are common operations in PythonVariables are a fine way to store data while your program is running, but if you want your data to persist even after your program has finished, you need to save it to a file. Python Training in Bangalore You can think of a file’s contents as a single string value,potentially gigabytes in size. In this you will learn how to use Python to create, read, and save files on the hard drive.
Files and File Paths
Files and file paths are essential concepts in programming, especially when dealing with file I/O operations. In Python,you often use the os module to work with file paths in platform-independent manner. A file has two key properties: a filename (usually written as one word) and a path. The path specifies the location of a file on the computer. For example,there is a file on my Windows 7 laptop with the filename projects.docx in the path. C:\Users\asweigart\Documents. The part of the filename after the last period is called the file’s extension and tells you a file’s type. project.docx is a Word document, and Users, asweigart, and Documents all refer to folders (also called directories).Python Training Certification in Bangalore Folders can contain files and other folders. For example, project.docx is in the Documents folder, which is inside the asweigart folder, which is inside the C:\ part of the path is the root folder, which contains all other folders.On Windows, the root folder is named C:\ and is also called the C: drive. On OS X and Linux, the root folder is /. In this book, I’ll be using the Windows-style root folder, C:\. If you are entering the inter-active shell examples on OS X or Linux,enter / instead.Additional volumes, such as a DVD drive or USB thumb drive, will appear differently on different operating systems. On Windows, they appear as new,lettered root drives, such as D:\ or E:\. On OS X, they appear as new folders under the /Volumes folder. On Linux, they appear as new folders under the /mnt (“mount”) folder. Also note that while folder names and filenames are not case sensitive on Windows and OS X, they are case sensitive on Linux.
Backslash on Windows and Forward Slash on OS X and Linux
On Windows, paths are written using backslashes (\) as the separator between folder names. OS X and Linux, however, use the forward slash (/)as their path separator. If you want your programs to work on all operating systems, you will have to write your Python scripts to handle both cases.
Fortunately, this is simple to do with the os.path.join() function. If you pass it the string values of individual file and folder names in your path,os.path.join() will return a string with a file path using the correct path separators. Enter the following into the interactive shell:
The Current Working Directory
Every program that runs on your computer has a current working directory,or cwd. Any filenames or paths that do not begin with the root folder are assumed to be under the current working directory.Python Online Training in Bangalore You can get the current working directory as a string value with the os.getcwd() function and change it with os.chdir(). Enter the following into the interactive shell.
Absolute vs. Relative Paths
There are two ways to specify a file path.
• An absolute path, which always begins with the root folder
• A relative path, which is relative to the program’s current working
directory
There are also the dot (.) and dot-dot (..) folders. These are not real folders but special names that can be used in a path. A single period (“dot”) for a folder name is shorthand.
Conclusion
In 2024,Python will be more important than ever for advancing careers across many different industries. As we've seen, there are several exciting career paths you can take with Python , each providing unique ways to work with data and drive impactful decisions. At NearLearn, we understand the power of data and are dedicated to providing top-notch training solutions that empower professionals to harness this power effectively.One of the most transformative tools we train individuals on isPython.
Comments
Post a Comment