import os

os.chdir('/home/israel/Development/Python_Exercises/python-octo-wookie/head1stpython/Chapter3')
#Change path for the current directory

data = open('sketch.txt')


#Start iteration over the text file
for each_line in data:
            try:
                (role, line_spoken) = each_line.split(':', 1)
                print(role, end = '')
                print(' said: ', end = '')
                print(line_spoken, end = '')
            except:
                pass

data.close()
        

import os

#Change path for the current directory
os.chdir('/home/israel/Development/Python_Exercises/python-octo-wookie/head1stpython/Chapter3')

#Check if file exists
if os.path.exists('sketch.txt'):
    
    #Load the text file into 'data' variable
    data = open('sketch.txt')

    #Start iteration over the text file
    for each_line in data:
    #We use try/except to handle errors that can occur with bad input
                try:
                    (role, line_spoken) = each_line.split(':', 1)
                    print(role, end = '')
                    print(' said: ', end = '')
                    print(line_spoken, end = '')
                except:
                    pass

    #After all the iteration and printing, we close the file
    data.close()

#If file does exists, we simply quit and display an error for the user/dev
else:
    print('The data file is missing!')
        

Validate if the file exists (if/else)
Validate if the file exists (if/else)

  import os
  
+ #Change path for the current directory
  os.chdir('/home/israel/Development/Python_Exercises/python-octo-wookie/head1stpython/Chapter3')
- #Change path for the current directory
  
+ #Check if file exists
+ if os.path.exists('sketch.txt'):
+     
+     #Load the text file into 'data' variable
- data = open('sketch.txt')
+     data = open('sketch.txt')
  
+     #Start iteration over the text file
+     for each_line in data:
+     #We use try/except to handle errors that can occur with bad input
+                 try:
+                     (role, line_spoken) = each_line.split(':', 1)
+                     print(role, end = '')
+                     print(' said: ', end = '')
+                     print(line_spoken, end = '')
+                 except:
+                     pass
  
+     #After all the iteration and printing, we close the file
+     data.close()
- #Start iteration over the text file
- for each_line in data:
-             try:
-                 (role, line_spoken) = each_line.split(':', 1)
-                 print(role, end = '')
-                 print(' said: ', end = '')
-                 print(line_spoken, end = '')
-             except:
-                 pass
  
- data.close()
+ #If file does exists, we simply quit and display an error for the user/dev
+ else:
+     print('The data file is missing!')
          
  
Validate if the file exists (if/else)
## Code Before:
import os

os.chdir('/home/israel/Development/Python_Exercises/python-octo-wookie/head1stpython/Chapter3')
#Change path for the current directory

data = open('sketch.txt')


#Start iteration over the text file
for each_line in data:
            try:
                (role, line_spoken) = each_line.split(':', 1)
                print(role, end = '')
                print(' said: ', end = '')
                print(line_spoken, end = '')
            except:
                pass

data.close()
        

## Instruction:
Validate if the file exists (if/else)
## Code After:
import os

#Change path for the current directory
os.chdir('/home/israel/Development/Python_Exercises/python-octo-wookie/head1stpython/Chapter3')

#Check if file exists
if os.path.exists('sketch.txt'):
    
    #Load the text file into 'data' variable
    data = open('sketch.txt')

    #Start iteration over the text file
    for each_line in data:
    #We use try/except to handle errors that can occur with bad input
                try:
                    (role, line_spoken) = each_line.split(':', 1)
                    print(role, end = '')
                    print(' said: ', end = '')
                    print(line_spoken, end = '')
                except:
                    pass

    #After all the iteration and printing, we close the file
    data.close()

#If file does exists, we simply quit and display an error for the user/dev
else:
    print('The data file is missing!')
        
