Help opening file

I’m trying to open a file in my python program, but it keeps saying the file doesn’t exist and that process_file is not defined. Please help I really need feedback. COP1000C is the name of the file.

def process_file(COP1000C):
        with open('C:\Users\Admin\OneDrive\Documents\COP1000C.txt', 'r') as file:
            lines = file.readlines()
            course_info = lines[:3]
            students = lines[3:]
            course_number_description = course_info[0].strip()
            instructor_name = course_info[1].strip()
            term = course_info[2].strip()
            student_records = [students[i:i+2] for i in range(0, len(students), 2)]
            grades = [int(record[1]) for record in student_records]
            passed_students = len([grade for grade in grades if grade >= 60])
            failed_students = len(grades) - passed_students
            # Calculate passing percentage
            passing_percent = round((passed_students / len(grades)) * 100)
            # Calculate average grade
            average_grade = round(sum(grades) / len(grades))

            console_width = 50
            title = "College Grades Summary"
            print("_" * console_width)
            print("_" * console_width)
            print(f"Professor: {instructor_name}\tTerm: {term}\n")
            print("Student Name\t\t\tGrade")
            for record in student_records:
            print("\nStudents' Performance")
            print(f"Passed: {passed_students}\tFailed: {failed_students}")
    except (IOError, FileNotFoundError) as e:
        # Handle exceptions related to file processing and print an error message
        print(f"Error processing file: {e}")

    filename = input("Enter name of course file: ")
    another_file = input("Would you like to process another file (y/n)? ")
    if another_file.lower() != 'y':

Hmm. I don’t think that you actually need the filename as an argument to the function.

You can just try:

def process_file():

A small trick I also learned on this forum:

from pathlib import Path

# Define your path 
my_file_path = Path('/Users/Admin/OneDrive/Documents/COP1000C.txt')

Then, when you are opening the file for reading, you can just call it via the following command:

with open(my_file_path, 'r', encoding = 'UTF-8') as file:

Almost certainly the problem is with the file name in:

Short answer: spell it like this:




Slightly longer answer: a backslash in a string introduces an escape sequence, so you can write things like a newline character as '\n'. Any place there is a '\', the compiler treats the next character (or several characters) as special. To get an actual backslash, you make the next character another backslash like this: '\\'. Or you can prefix the string with an “r” (for raw) to turn off that behaviour. Full story here: 2. Lexical analysis — Python 3.12.2 documentation

It is only really a problem when typing Windows file names and “regular expressions”.