0

My Code

File From Teacher

I've tried anything I can think of for "Task 1" which is written in the green comment. Also, when I downloaded "sample.txt" it downloaded as "sample-1.txt" as its name but I'm not sure if it needs the second ".txt" in the code. Thank you.

Code:

"""Task 1: Write a program to read each line from the file sample.txt, and print your original contents of the file.

Task2: Add the following line to the sample.txt file :
    "Aaron Woods 1122 123 324 45 88 1561 9 18"
     and print your new updated file.
     
Task 3: Define a function that returns a list that contains information as follows:
    [['Cobb' , 'Ty' , 3747.5],[],..,['Smoltz' , 'John' , 293.5],['Woods' , ' Aaron' , 624.00]]
    where , 3747.5 is an average calculated by:
        Ty Cobb => avg = (13099+11434+3053+724+295+117+1249+9)/8
    Same way calculate average for all other players and build the list in the function and return that to main.
    Also, note that the last name and first name positions have changed.
    
Task4: Sort the new list returned by the function, such that the player having least average is printed first and the highest average is printed last."""
#Task 1
file = open("sample-1.txt.txt", "w")
print(file)
file.close()
7
  • Please don't post images of code. Post the code itself as well-formatted text. In any event -- there is probably no good reason to have a text file name ending with .txt.txt, so that shouldn't appear in your code. Commented Nov 24, 2022 at 1:40
  • @JohnColeman, as shown in the picture of the file, the file itself has ".txt.txt" as it's name. So I'm not sure if that's the issue. Commented Nov 24, 2022 at 1:46
  • Does this answer your question? How do get Python to print the contents of a file Commented Nov 24, 2022 at 1:46
  • If the file is now local to your machine -- just rename it. That seems to be some glitch in how the file was downloaded. I doubt that your teacher is using a bizarre naming convention. Commented Nov 24, 2022 at 1:48
  • @NickODell Unfortunately, no. It's saying it's an unsupported operation. Commented Nov 24, 2022 at 1:58

1 Answer 1

1

file is a file object, not the text contained in the file. If you just want to print the contents of the file use print(file.read())

If you want to iterate over every line in the file then this is a very common way of doing so:

with open("sample-1.txt.txt", "r+") as file:
    for line in file:
        # do stuff here

Using the with is so you don't need to remember to close the file afterwards.

Sign up to request clarification or add additional context in comments.

5 Comments

It's saying it's an "Unsupported Operation".
You'll need to change the file mode from w to r if you want to read it.
Now the error is gone but it is back to printing blank.
If you open the file with "w" it overwrites the existing file with a blank one (I should change that in my text block). You probably need to re-download the txt file at this point.
Another problem might be the ".read()" part. The program we're assigned to use doesn't understand that. Our professor told us to use "print("first line", file = my_file)" for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.