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

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



Copyright Disclaimer:
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.