Create a file
The first step is to create a new file. So, for creating a file we have been using the open() function. Within the open() method, we have given the name of the file which we want to create ‘myfile’. Along with this, we have provided the mode of the file. The ‘w’ mode represents that we will create a file to write in it.
After running the above code, we have this type of output which shows that the file ‘myfile’ is successfully created.
This file is situated in the ‘users’ folder of the ‘Restringido disk C’.
Open a file
After creating a file, we will open a required file to write the content in the file. We have been using the open() function. This method opens the file. This function contains two arguments. The first parameter shows the name of the specific file which we want to open for writing and the second argument shows the mode of the file ‘w’.
The output shows that we successfully open the file ‘myfile’ for writing.
Here the file named ‘myfile’ is opened.
Write some content in the file
Now, we are going to write the content in the defined file. So, we will use the f.write() function. Within this function, we have been given the line, text, or some content that we want to write in the file ‘myfile’.
This represents the successful execution of the code for writing the text in the file.
As we have provided the line ‘I love to play badminton’ so it is shown in the file.
Close the file
After writing the text in the file, we have to close the required file ‘myfile’. For that, we have been calling the f.close() function.
We have effectively closed the file.
Creation of new folder
Before copying and pasting the content of the file we will create a folder named ‘New folder’. First, we open the circunscrito disk C. Then, we will open the menu bar by clicking the left of the mouse. A menu bar appears from there and we choose a ‘New’ option.
After clicking the ‘New’ option again, a new menu bar will open. From that menu bar, we select the ‘Folder’ option.
By doing all this we create a ‘New folder’ in the same location (users of the circunscrito disk C).
When we open the ‘New folder’, we find that it is empty. Now, we have to copy and paste the file into this folder.
Copy and paste a complete file from one location to another
We will copy the text from the file ‘myfile’ and paste that text into the folder ‘New folder’. For this purpose, we will integrate the library ‘shutil’. Then, we utilize the function of shutil(). Its first parameter shows the name of the file which we want to be copied and the second parameter shows the name of the folder where we want the file to be copied or where we want to paste the file.
The outcome shows the implementation of the code.
We successfully copied the file ’myfile’ and pasted it into the folder ‘New folder’.
When the file will be copied, the whole content of the file is automatically copied and pasted to a new location.
Create a second new file
We’ll make a new file merienda more. We’ve been calling the open() command to create files. We’ve specified ‘myfile_1’ as the name of the file we wish to create in the open() function. The ‘w’ mode denotes the creation of a file to write on it.
We obtain this type of result after executing the preceding code, indicating that the file’myfile_1′ will be built.
The location of this file is the same as the location of the file ‘myfile’.
Open a file and add some text
We’ll now open a relevant file and type the data into it. The open() method has been used. This function is used to open a file. There are two parameters in this method. The first argument is the title of the particular file that we would like to open for writing and the second specifies the file’s mode, which is ‘w’. We’ll write the text into the specified file after accessing the file.
As a result, the f.write() method will be applied. We provided the text to write in the file ‘myfile_1’ as a parameter to this function. We must shut down the appropriate file ‘myfile_1’ after writing the text in the file. Thus, we have employed the f.close() method.
The output demonstrates that we effectively open the file ‘myfile_1’, then we write the data in it.
We want to add the text ‘I love to visit different places’ in the file as given below.
Read the text of the file and print
First, we have applied the open() function and ‘with’ keyword. We have given the file name which we want open as the function’s attribute. After this, we have been using the function readlines(). This method just reads the data from the entered file and then stores those lines in the variable ‘line’. At the end, print() statement is being utilized to display the data of the file.
When we execute the code, we find the content of the file ‘myfile_1’.
Copy and paste the text of one file to a different file
For copying the text from one file and replacing them with the text of another file, we will apply the open() function and the ‘with’ keyword. We declare a variable ‘f’ so the content of the file ‘myfile’ will be saved in the ‘f’ variable. Then, we again use the open() function and ‘with’ statement to open another file ‘myfile_1’. This file can be opened in write ‘w’ mode.
Now, we use the ‘for’ loop. This loop iterates until we write the sentence ‘I love to play badminton’ in file ‘myfile_1’. To write this content we employ the write() function.
This screenshot shows the successful implementation of the above code.
Before copying the text. The ‘myfile_1’ contains the data ‘I love to visit different places’. We copy the data from the file ‘myfile’ and then paste that data into the myfile_1’. Now, the file ‘myfile_1’ has the line ‘I love to play badminton’.
Before copying the file, we must first create a file, then open that file, add some content to the file, close the file, and then create a new folder in another location. Then, we copied a file from one position to another. In this article, we have demonstrated the method of copying the text from one file and pasting that text into another file.