[no audio]
Friends here, we are going to see how to copy a file into another file
using Python, or simply copy the content of a file to another
file. Let us assume that you have a file with some content
in one location, in any location in your host,
and whatever the data you have in that file that data
I want to copy to some another file which is there in maybe
same location or in different location.
So this entire data I want to copy into this file. Assume
that this is a source file and this is a destination file.
So what you are doing guys? Simply the content which is there
in your source file, that content you're going to write it
into destination file.
[no audio]
Now first step what you have to do? Read the content of your
source file, then whatever the data you read from your source
file that data just write it into destination file.
You know, if you want to read a data from any file in your
Python you have to open that file in 'read' mode.
And if you want to write any content to a file, then you
have to open that file in 'write' mode.
[no audio]
'read' mode, you can able to read. 'write' mode, the content which
you have in your hand that content you can write it into
your file. That's it.
So, let me open my editor just to write your Python Script.
So what I am doing is simply I am going to save
a Python Script name as
[no audio]
'source_to_destination.py'.
So let me take
first of all source file.
You can take any file. As of now
let me take my file as,
let's say
[no audio]
Yeah, I have somewhere
file. Let me take that.
Suppose if you observe here
I have a file called 'random'. In that
I have some three lines.
Let me take this file name first.
[no audio]
'random.txt', and I want
to copy this content to some destination file.
Let's say destination file name equals to 'newrandom.txt'.
So first of all source file content you have to read,
then whatever the data you are reading from source file
that data we need to write into destination file.
So what I am doing is, let me read the content
from your source file first.
So 'sfo=', open that source file in
[no audio]
'r' mode, then let me close your opened file.
'sfo.close'. Let me save it and run it.
[no audio]
You're not getting anything. You opened your file in 'r' mode,
but where is your data?
See if you want to see the data,
if you want to read the data from your source file, you know
any variable, or let me say 'content=', your
'sfo.read', read entire data as a string.
You know you have different operations 'read', 'readline',
'readlines'. So 'read' is a string, 'readline' is a string,
'readlines' is a list.
So I am going to read entire data as a string.
Yes, you can read it.
Now, let me print the data from your source file,
whatever the data you are reading.
[no audio]
Yes, that's it. But this data I want to write it into
my destination file.
Nothing is there. It's very simple. If you want to write any
data into a file what you have to do?
Just open that file.
Let me take this 'dfo= open(, and then
destination file. Guys,
our file name is already there in this variable. Then 'w'
mode, then close that file.
[no audio]
But before going to close you have to write the content in
your destination file.
That's why, your 'dfo.write', already you have in
your hand the content of your source file, that is with
the variable called 'content'. Save it.
Done. Let me run it. Done.
Let me see that there is a 'newrandom' file in our location.
[no audio]
Let me search with 'n'.
[no audio]
Yeah, 'newrandom' is there. That's it.
Right, and one more thing guys.
Suppose this 'random.txt'
I am copying into Desktop location.
[no audio]
Right. Fine.
Now, if you observe your Python Script, this Python Script is there
in suppose some different location. 'PythonScripts' directory
you have your Python Script. And I'm running. See that. What
you are getting? You're getting an error like, while opening
your source file, you are getting an error like
'No such file or directory'.
The reason is your Python Script
is there somewhere in 'PythonScripts' directory. Under Desktop
you have a directory called 'PythonScripts', under that you
have your Python Script. But your 'random.txt' is not
there in the location where you have your Python Script. That
is there in a different location called Desktop.
So that's why whenever if you are trying to read or write,
or whenever if you are working with any files try to provide
complete path.
Right. See, now
what I am doing is, I am going to take complete path. Let's say
[no audio]
I need 'Desktop' path.
[no audio]
So guys, this is Windows,
that's why whenever if you are giving a path, please provide
'\\' in your path.
[no audio]
That's it.
Now destination file location simply you are giving file name,
but where you want to store exactly?
If you don't provide any complete path, then the destination
file will create in the location of where your Python Script
is there. From which location you are running your Python
Script, in that location only it will create. Now
I want to create my Python,
I mean my destination file in a different location called,
suppose 'Downloads'.
Right. So I want to create my destination file called 'Downloads'
location. Let's say, copy that.
[no audio]
Provide the complete path, 'Downloads'.
So be clear, whenever if you are working with files path,
you have to provide '\\' in your file path in your
Windows. Now, let me save it and before going to run
observe that in Desktop
you don't have any file called 'newrandom.txt'. Now,
I am going to save it and run it.
[no audio]
Python Script has completed. Observe that
There is a file created 'newrandom'.
That's it.
And as of now whatever the Python Script you have in your
hand that is always going to work with fixed file, 'random',
and then this is going to create a destination file called
'newrandom.txt'. But I don't want to do in this way. Whenever
if I run my Python Script it has to ask, "Enter your source
file", "Enter the destination file", then that's good.
So that's why what I am doing is, simply I am going to comment
these two lines. Now instead of these two lines
what I am writing is 'sfile=', I want to read
the source file location while running your script. "Enter
your source file location or source file".
Now this script is like a dynamic script.
It will work on any required files, right. "Enter your destination.
file". That's it. Save it.
Go to command line. Guys, in Sublime Text you can't provide input,
that's why I'm going to run my Python Script from,
[no audio]
let's say we have our Python Script somewhere here.
Our Python Script is source to destination, 's_d.py'.
Now, I am going to run this.
[no audio]
So what I am doing is I want to copy our Python Script itself.
Just assume that. I want to copy our Python Script itself
[no audio]
into some different location.
[no audio]
Let me copy complete path then.
So in this location we have our Python Script, and
let me provide '\\
[no audio]
PythonScripts\\s_d.py'. This file
I need to copy, this file content
I need to copy into some different location with same name
or different name, that is your wish.
Let's say I want to move this into suppose for 'Desktop' location.
I want to copy this file into 'Desktop' location. In the 'Desktop'
I want to provide same python script name,
yes you can, or with some different name
also you can provide. Just let's say I am taking same file
name. Okay. 'Enter'. Done.
Go to your 'Desktop', now search for 's_d'.
Yes, it has been created. Open that file and see the content.
[no audio]
You got it. Right. So simply this is a Python Script just
to copy source file content into destination file.
Right. And guys you may get doubt, suppose if you go with Unix
or Windows there are some commands like 'cp source
[no audio]
destination'. Single line code is enough,
right. But why we are writing this many number of lines
to copy your source content to destination file?
[no audio]
Right. So as of now, there is no much usage,
but while going forward this concept is very, very helpful,
very, very useful.
So that's why just I have given introduction to copy one
file content into another file content, right, here. And one
more thing, last point.
So by default guys, by default, if you don't open your file
in read mode, I mean if you don't provide any mode while
opening your file, by default that is the read mode. If you
want to verify, let's say 'print()',
your 'sfo.,
what is the mode for that. Right.
Now, what I am doing is, I am going to just run here itself.
So I'm going to provide fixed filenames, observe the output.
You're getting 'r'. 'r', that means if you open your file without
mentioning any mode, by default that is the 'read' mode. That's it.
[no audio]
Right. Okay.
Okay guys, thank you for watching this video. While going
forward we will use this concept to work with some real time
scenarios. Okay.
[no audio]