Python: Guide to Write to a File
While learning any programming language, writing to file in Python becomes one of the essential tasks. It allows the developers to manipulate large data sets, save data that can be used for reference in the future, and for building essential tools. Python includes an in-built feature of reading, writing, and creating files in either standard text or binary files. Comparatively, text files are easy to create. Each line of this file terminates with the End of Line (EOL) which, by default, creates a new character in Python. Whereas, a binary file lacks terminator for the line, which stores the data after it gets converted into a language that’s understandable for the machine. Here are some types of files that one can write in Python.

To write in ‘.txt’ file
As we mentioned earlier, Python allows easy writing of files by creating a text file. Writing this type of file will enable you to save any string to extract information from it later. Creators must begin with opening a file and entering the valuable content. Once done with the procedure, they must close the file. You can take reference from the following example:
myFile = open(“NewFile.txt”, “w+”)
myFile.write(“Good Morning!”)
myFile.close()
Here, “w+” means that we are opening and writing to a file that is new. If you use a pre-existing file, then it will be overwritten. For a non-existent file, the file will be created. If you want to add something to the pre-existing file, then write “a+” instead of “w+”.
To write in different file format
Other than text files, you can write and create various other files as well, but you must know how a specific file-type uses formatting. You can use Comma Separated Values (CSV) to write to files and save data. Each line of the CSV signifies a row in the database, and it also consists of a sequence of values. The database and values are further separated by commas for starting a new column or cell. Please ensure that you insert the commas in the appropriate place, and remember that all the files under the ‘.CSV’ will open in an Excel sheet. Similarly, you can also create an HTML file using the correct triangular tags for defining headers, texts, and other formats in the file. This way, all the experienced developers customize their file formats for storing specific data.
To write a file with modules
There are some files that use a more complex format than the ones mentioned above. The ‘.doc’ file in Python is one of them. Using a module simplifies the whole procedure. If you open an MS Word document, you might get confused due to the complicated formatting and annotations for defining the layout and adding more information. From pip, install a module and do the following in your Python coding:
import docx
mydoc = docx.Document()
mydoc.add_paragraph(“Good Morning!”)
mydoc.save(“D:/NewHelloDoc.docx”)
You will find “Good Morning” written in your document. For more advanced formatting, try this:
mydoc.add_heading(“Header 1”, 0)
mydoc.add_heading(“Header 2”, 1)
mydoc.add_picture(“D:/MyPicture.jpg”,width=doc.shared.Inches(4), height=docx.shared.Inches(6))
You can find various modules to support complex formatting and writing. You can get them for free along with documents to go through them. We would recommend the beginners take classes for understanding the complexities of Python and work on it effortlessly. This will also teach you to delete and move files from Python.
Mila is a Blog expert and has been working in the technology industry since 2003. As a technical expert, Mila has written technical blogs, manuals, white papers, and reviews for many websites such as
Source:
Comments
Post a Comment