Python for Beginners: The Crash Course to Learn Python Programming in 3-Days (or less). Master Artificial Intelligence for Data Science and Machine Learning + Practical Exercises by Khan Aaron
Author:Khan, Aaron [Khan, Aaron]
Language: eng
Format: epub
Published: 2020-07-19T16:00:00+00:00
Chapter 3
The Files
In this chapter, we are going to spend some time looking at how you can deal with the input and output of files within Python. There are a lot of different things that you are able to do with this, and you will need to be able to bring all of them out at some point or another as you are working on your code. The four things that we are going to look at is how to create a new file, how to close a file, how to look for and move a file, and how to write on a file that you have already created and saved
Creating a new file
The first option that we are going to take a look at in this chapter is how to create a new file for your needs. If you would like to make a new file that you can write out code on, then you need to open up the IDLE and then choose the mode you want to use. There are going to be three modes that work with creating a file including mode(x), write(w), and append(a).
Any time that you are looking to make some changes to the file that you have opened, you want to use the write method because this one is easiest for you to use. And if you would like to open up a file and get a new string written in that file, you would also need to work with the write method. This ensures that everything goes in the right place and the characters are going to be returned by the compiler.
You will be able to use the write() function on a regular basis because it is easy and allows the programmer to come in and make any of the changes that they want to the file. You can add in some new information, change up some of the information you have, and so on. To look at how the code is going to appear in the compiler when you are working with the write() function, use the code below:
#file handling operations
#writing to a new file hello.txt
f = open(‘hello.txt’, ‘w’, encoding = ‘utf-8’)
f.write(“Hello Python Developers!”)
f.write(“Welcome to Python World”)
f.flush()
f.close()
In addition to being able to create and write on a file that is brand new, there may be times when you need to go through and overwrite some of the information that you have to ensure that a new statement, or a new part, shows up that wasn’t there before. Python does allow for it, and the code that you need to use to make this happen will be below:
#file handling operations
#writing to a new file hello.txt
f = open(‘hello.txt’, ‘w’, encoding = ‘utf-8’)
f.write(“Hello Python Developers!”)
f.write(“Welcome to Python World”)
mylist = [“Apple”, “Orange”, “Banana”]
#writelines() is used to write multiple lines into the file
f.write(mylist)
f.flush()
f.close()
The next thing that we can work on doing is binary files. This is simple to do because it is going to take the data that you have and change it over to a sound or an image file instead of a text file.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12566)
Hello! Python by Anthony Briggs(9911)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9336)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8293)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7032)
Microservices with Go by Alexander Shuiskov(6797)
Practical Design Patterns for Java Developers by Miroslav Wengner(6711)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6650)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Angular Projects - Third Edition by Aristeidis Bampakos(6059)
The Art of Crafting User Stories by The Art of Crafting User Stories(5589)
NetSuite for Consultants - Second Edition by Peter Ries(5524)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5328)
Kotlin in Action by Dmitry Jemerov(5062)
