<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Up until now, we dealt with very little data created by us or asked from user, and we stored them in lists or variables. In this lecture you will be introduced to file handling in Python.
After this lecture you will be able to;
Open/Close files
Read/Write files
Utilize text files in Python
Perform string processing in Python
Text File vs Binary File
A text file is a file containing characters, structured as individual lines of text. In addition to printable characters, text files also contain the nonprinting newline character, \n, to denote the end of each text line. 
Text files can be directly viewed and created using a text editor.
In contrast, binary files can contain various types of data, such as numerical values, and are therefore not structured as lines of text. Such files can only be read and written via a computer program.
Handling Files
Fundamental operations of all types of files include opening a file, reading from a file, writing to a file, and closing a file. Next we discuss each of these operations when using text files in Python.
All files must first be opened before they can be used. In Python, when a file is opened, a file object is created that provides methods for accessing the file.
The open function opens the given file with r reading access.
Step1:  When you try to print the variable created, you will not get what you want. It is just an object created to use in later statements. 
To open a file the open function used with parameter 'w'
Step2:  We won't get any error because we are creating a new file only error we might get is harddisk full error from system. After we are done manipulating the file we have to close the file. with close() function.
Step3:  Reading Files
The readline method returns as a string the next line of a text file, including the end-of-line character, \n.When the end-of-file is reached, it returns an empty string
Step4:  I used while loop to show the logic behind the reading, however for loop gives us a more elegant way.
Step5:  Writing Files
The write method is used to write strings to a file
Step6:  The write method does not add a newline character to the output string . Thus, a newline character will be output only if it is part of the string being written. But in the example above line variable comes with \n at the end. 
String Processing
The information in a text file, as with all information, is most likely going to be searched, analyzed, and/or updated. Collectively, the operations performed on strings is called string processing.
We have learned some basic operations on strings such as 
accessing elements
Step7:  There are a number of methods specific to strings in addition to the general sequence operations.
Checking the Contents of a String
Step8:  str.isalpha()
Step9:  str.isdigit() Returns True if str contains only digits.
Step10:  str.islower()  and str.isupper() 
Step11:  str.lower() and str.upper()
Step12:  Searching the Contents of a String
str.find(w)
Step13:  Replacing the Contents of a String
str.replace(w,t)
Step14:  Removing the Contents of a String
str.strip(w)
Step15:  Splitting a String
str.split(w)
<ASSISTANT_TASK:>
Python Code:
input_file = open('sample.txt', 'r') # IOError occured because we should put the write true direction.
input_file = open('data/sample.txt', 'r') # True direction, with folder inside.
print(input_file)
input_file.close()
Explanation: Up until now, we dealt with very little data created by us or asked from user, and we stored them in lists or variables. In this lecture you will be introduced to file handling in Python.
After this lecture you will be able to;
Open/Close files
Read/Write files
Utilize text files in Python
Perform string processing in Python
Text File vs Binary File
A text file is a file containing characters, structured as individual lines of text. In addition to printable characters, text files also contain the nonprinting newline character, \n, to denote the end of each text line. 
Text files can be directly viewed and created using a text editor.
In contrast, binary files can contain various types of data, such as numerical values, and are therefore not structured as lines of text. Such files can only be read and written via a computer program.
Handling Files
Fundamental operations of all types of files include opening a file, reading from a file, writing to a file, and closing a file. Next we discuss each of these operations when using text files in Python.
All files must first be opened before they can be used. In Python, when a file is opened, a file object is created that provides methods for accessing the file.
The open function opens the given file with r reading access.
End of explanation
output_file = open('data/mynewfile.txt', 'w') # I used data/name because I want to save in data folder
Explanation: When you try to print the variable created, you will not get what you want. It is just an object created to use in later statements. 
To open a file the open function used with parameter 'w'
End of explanation
output_file.close()
Explanation: We won't get any error because we are creating a new file only error we might get is harddisk full error from system. After we are done manipulating the file we have to close the file. with close() function.
End of explanation
input_file = open('data/sample.txt', 'r')
empty_str = ''
line = input_file.readline() 
while line != empty_str:
    print(line)
    line = input_file.readline()
input_file.close()
Explanation: Reading Files
The readline method returns as a string the next line of a text file, including the end-of-line character, \n.When the end-of-file is reached, it returns an empty string:
End of explanation
input_file = open('data/sample.txt', 'r')
for line in input_file:
    print(line)
Explanation: I used while loop to show the logic behind the reading, however for loop gives us a more elegant way.
End of explanation
empty_str= ''
input_file = open('data/sample.txt', 'r')
output_file = open('data/newfile.txt', 'w')
line = input_file.readline()
while line != empty_str:
    output_file.write(line)
    line = input_file.readline()
    
output_file.close()
Explanation: Writing Files
The write method is used to write strings to a file:
End of explanation
space = ' '
num_spaces = 0
line = input_file.readline()
for k in range(0, len(line)):
    if line[k] == space:
        num_spaces = num_spaces + 1
num_spaces
Explanation: The write method does not add a newline character to the output string . Thus, a newline character will be output only if it is part of the string being written. But in the example above line variable comes with \n at the end. 
String Processing
The information in a text file, as with all information, is most likely going to be searched, analyzed, and/or updated. Collectively, the operations performed on strings is called string processing.
We have learned some basic operations on strings such as 
accessing elements: name[k]
getting the length: len(str)
String Traversal: The characters in a string can be easily traversed, without the use of an explicit index variable, using the for chr in string form of the for statement.
End of explanation
s = 'Hello World!'
Explanation: There are a number of methods specific to strings in addition to the general sequence operations.
Checking the Contents of a String
End of explanation
s.isalpha() #
Explanation: str.isalpha():  Returns True if str contains only letters
End of explanation
s.isdigit()
"1".isdigit()
Explanation: str.isdigit() Returns True if str contains only digits.
End of explanation
s.islower()
s
s.isupper()
"HELLO WORLD".isupper()
Explanation: str.islower()  and str.isupper() : Returns True if str contains only lower/upper case letters
End of explanation
s
s.upper()
s.lower()
s # Does not change... You have to assign it to an new variable or overwrite
s = s.lower()
s
Explanation: str.lower() and str.upper(): Returns lower/upper case version of str
End of explanation
s
s.find('d')
s.find('x')
Explanation: Searching the Contents of a String
str.find(w): Returns the index of the first occurrence of w in str, Returns -1 if not found
End of explanation
s
s.replace("l", "*")
Explanation: Replacing the Contents of a String
str.replace(w,t): Returns a copy of str wita ll occurrences of w replaced with t.
End of explanation
s
s.strip('!')
Explanation: Removing the Contents of a String
str.strip(w): Returns a copy of str with all leading and trailing characters that appear in w removed.
End of explanation
s
s.strip('!').split(" ")
s[:-4]
Explanation: Splitting a String
str.split(w): Returns a list containing all strings in str delimited by w:
End of explanation
<END_TASK>