This is where I am at so far and keeps giving me an error Ca



This is where I am at so far and keeps giving me an error. Can you please do this step by step using python.
file-open(filename,r ileNotFoundError: [Errno 2] No such file or directory: \'f1.txt > def fileRead(filename): file--open(filename, \'r) filecontent-file.read) return filecontent >> def filewrite(filename, filecontent): file-open(fileName, \'w\") file.write(filecontent) file..closeC) file1content-fileReadC \'f1.txt\') Traceback (most recent call last): File \"\", line 1, in file1content-fileRead( File f1. txt\' in \"\", line 2, fileRead file--open(filename, \'r) FileNotFoundError: [Errno 2] No such file or directory: \'f1.txt\" 2 Ln 1073 20

Solution

The code written is correct only, the error you are getting is because the file which you are given to read f1.txt is not present in your present working directory. In order this program to work either you have to give proper path of the file f1.txt or move the f1.txt file to your present working directory.

Please find the required program along with its output. Please see the comments against each line to understand the step.


def fileRead(filename):
file_ = open(filename,\'r\')
filecontent = file_.read()
return filecontent
  
def fileWrite(filename,filecontent):
file_ = open(filename,\'w\')
file_.write(filecontent)
file_.close()
  
file1content = fileRead(\'f1.txt\') #read contents of the file f1.txt and save it to variable filecontent
fileWrite(\'f2.txt\',file1content) #write the variable filecontent\'s data to a new f2.txt file

------------------------------------------

OUTPUT:

Files in present working directory:

sh-4.3$ ls f1.txt  main.py

Fie f1.txt content:

sh4.3$ cat f1.txt   

Hi Hello...

Files in present working directory after program ran :

sh-4.3$ python main.py
sh-4.3$   
sh-4.3$ ls
f1.txt f2.txt main.py   
sh-4.3$

File f2.txt(created after program ran) contents :
sh-4.3$ cat f2.txt
Hi Hello...   
sh-4.3$

 This is where I am at so far and keeps giving me an error. Can you please do this step by step using python. file-open(filename,r ileNotFoundError: [Errno 2] N

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site