how to run a python file code example

Example 1: python open file

with open('filename', 'a') as f: # able to append data to file
	f.write(var1) # Were var1 is some variable you have set previously
	f.write('data') 
	f.close() # You can add this but it is not mandatory 

with open('filename', 'r') as f: # able to read data from file ( also is the default mode when opening a file in python)

with open('filename', 'x') as f: # Creates new file, if it already exists it will cause it to fail

with open('filename', 't') as f: # opens the file in text mode (also is defualt)

with open('filename', 'b') as f: # Use if your file will contain binary data
  
with open('filename', 'w') as f: # Open file with ability to write, will also create the file if it does not exist (if it exists will cause it to fail)
  
with open('filename', '+') as f: # Opens file with reading and writing

# You can combine these as you like with the + for reading and writing

Example 2: run python file using python code

exec(open('file.py').read())

Example 3: how to run a python script

# Save the script in a file with .py extension
# run the script using the below command
python fileName.py

Example 4: run python.py file

# save a file in text editor with .py
# open terminal, and change directories
# run the following command:
python3 file.py

Example 5: run python program from command line

# press [windows] + [R] (on windows only)
# use the command : cd/the_way_of_your_program
# and now use this command :

py the_name_of_your_program.py 

# don't forget to write the extension (py for python) 
# at the end of your program name

Example 6: how to run python program

move to the directory where your program lies 
open cmd 
python first_script.py(your file name)