Python help with opening file

I’m trying to open a file on my computer but it won’t open and is saying that the file doesn’t exist and I am also getting the error that “COP10000C” is not defined even though it is please help I need to bring my grade up.)

Start by reposting/editing after reading the pinned post: About the Python Help category

People are much less likely to help unless you follow those guidelines.


Posting a screenshot like that? Ouch! :slight_smile:

You should definitely do what @csm10495 says.

As for your code, it asks for the name of a file and then calls process_file with the value of COP1000C, which is a variable that has not been defined.

The function process_file, incidentally, always tries to open a file called “COP1000C.txt”. It doesn’t do anything with whatever was passed in via the parameter COP1000C. Why does the parameter have that name?

What the code should be doing is calling process_file(filename), the function should be defined with def process_file(filename), and that function should be opening the file with with open(filename) as file:.

1 Like

Hi, when I do that is says Error processing file: [Errno 2] No such file or directory: ‘COP1000C.txt’
Would you like to process another file (y/n)?

Did you type in “COP1000C.txt” as the filename?

If you did, then it’ll look for that file in a current directory, wherever that is.

You should enter the full path of the file.

Yes I put it like this

# Function to process a file and generate a course grades summary report
def process_file(filename):
        # Open the file in read mode
        with open('COP1000C.txt', 'r') as file:
            # Read all lines from the file
            lines = file.readlines()

            # Extract course information from the first three lines
            course_info = lines[:3]
            # Extract student records from the remaining lines
            students = lines[3:]

            # Extract individual course information
            course_number_description = course_info[0].strip()
            instructor_name = course_info[1].strip()
            term = course_info[2].strip()

            # Group student records (name and grade are consecutive lines)
            student_records = [students[i:i+2] for i in range(0, len(students), 2)]
            # Extract grades from student records and convert them to integers
            grades = [int(record[1]) for record in student_records]
            # Calculate number of passed students (grade >= 60)
            passed_students = len([grade for grade in grades if grade >= 60])
            # Calculate number of failed students
            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))

            # Set console width for printing
            console_width = 50
            # Set title for the report
            title = "College Grades Summary"
            # Print title with underscores on either side
            print("_" * console_width)
            print("_" * console_width)
            # Print course information
            # Print instructor name and term
            print(f"Professor: {instructor_name}\tTerm: {term}\n")
            # Print header for student records
            print("Student Name\t\t\tGrade")
            # Print line separator
            # Print each student record with name and grade
            for record in student_records:
            # Print header for performance summary
            print("\nStudents' Performance")
            # Print line separator
            # Print performance summary: number of passed and failed students, passing percentage, and average grade
            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}")

# Main loop to continuously process files until the user decides to stop
while True:
    filename = input("Enter name of course file: ")
    another_file = input("Would you like to process another file (y/n)? ")
    if another_file.lower() != 'y':


with open('COP1000C.txt', 'r') as file:

will always try to open “COP1000C.txt” in the current directory, wherever that is, no matter what was passed in.

Presumably, that file is not in the current directory.

1 Like

Yea I’m not sure how to get to it

You ask the user for the full path of the file, including the directory and the extension, and the program passes that to process_file:

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

process_file then opens the path that it was passed:

def process_file(path):
        # Open the file in read mode
        with open(path, 'r') as file:
            # Read all lines from the file
            ... # etc

So with this when asked anger path to course file do I type in COP1000c. Because when I did I still got Error number 2

When it asks for the full path, type in the full path, which, from the original screenshot, I see is C:\Users\Admin\OneDrive\Documents\COP1000C.txt.

I pasted it and I got error 22 now that says invalid argument

I tried adding different slashes to see if it made a difference

Please don’t post screenshots.

Copy and paste any code or traceback, and in order to preserve formatting, select the code or traceback that you posted and then click the </> button.

You’re supposed to type in only the path.

Don’t put quotes around it. It isn’t some code in a program.

1 Like

Thank you so much for the help it finally worked.