Posts

Showing posts with the label Python Files

learnpython24-(Python File I/O)

  Python File I/O In this tutorial, you'll learn about Python file operations. More specifically, opening a file, reading from it, writing into it, closing it, and various file methods that you should be aware of. Files Files are named locations on disk to store related information. They are used to permanently store data in a non-volatile memory (e.g. hard disk). Since Random Access Memory (RAM) is volatile (which loses its data when the computer is turned off), we use files for future use of the data by permanently storing them. When we want to read from or write to a file, we need to open it first. When we are done, it needs to be closed so that the resources that are tied with the file are freed. Hence, in Python, a file operation takes place in the following order: Open a file Read or write (perform operation) Close the file Opening Files in Python Python has a built-in  open()  function to open a file. This function returns a file object, also called a handle, as it...

learnpython24-(Python Directory and Files Management)

  Python Directory and Files Management In this tutorial, you'll learn about file and directory management in Python, i.e. creating a directory, renaming it, listing all directories, and working with them. Python Directory If there are a large number of files to handle in our Python program, we can arrange our code within different directories to make things more manageable. A directory or folder is a collection of files and subdirectories. Python has the  os  module that provides us with many useful methods to work with directories (and files as well). Get Current Directory We can get the present working directory using the  getcwd()  method of the  os  module. This method returns the current working directory in the form of a string. We can also use the  getcwdb()  method to get it as bytes object. >>> import os >>> os.getcwd() 'C:\\Program Files\\PyScripter' >>> os.getcwdb() b'C:\\Program Files...