Ex 16 runs, doesn't actually do anything

I’ve reproduced the code exactly as it is written in the book, and the script runs without triggering any errors, but it doesn’t actually change the target file. Anyone else had this problem? Anyone solved it?

Hello and welcome to the forum @forumcarrot16

I am sure I had some problem with this one too. Just don’t remember what.
The bigger mistakes was easy. They could not hide themselves. :slight_smile:
But most often there was a tiny thing that was very hard to spot.
If you would like to put up the code you have written here we can help to see whats missing or where the problem are.

# Copy and paste your code in between those brackets below 
your code...

There are a lot of friendly eyes in here :face_with_monocle: to help you.

This the script in full. I don’t know what could be going wrong but I also don’t really know a lot about writing code. I’ve tried using different files, creating and deleting several different files. I’ve run this in Powershell and CMD. The result continues to be that the script runs as it should but doesn’t appear to actually DO anything. Hopefully there’s an error in my code and its not just a problem with my finicky computer.

from sys import argv

script, filename = argv

print(f"We're going to erase {filename}.")
print("If you don't want that, hit CTRL-C (^C).")
print("If you do want that, hit RETURN.")


print("Opening the file...")
target = open(filename, 'w')

print("Truncating the file.  Goodbye!")

print("Now I'm going to ask you for three lines.")

line1 = input("line 1: ")
line2 = input("line 2: ")
line3 = input("line 3: ")

print("I'm going to write these to the file.")


print("And finally, we close it.")

Is the targetfile e.g. ‘filename’ in the same directory/folder as your ex16.py script?

Yes it is. The target file and my script are contained within the same folder.

I just ran your script and it is changing the file.

I have noticed at the very bottom of your code you are printing the filename argument:
print(filename) which just outputs the name of the textfile (in my case filename.txt)

When I looked at the actual file via the cat command on Powershell, it has indeed changed.

Is it the bottom line of code that is confusing you perhaps? View the filename text file via the cat command or open it in notepad after you have run the script to see if it has changed.

I put that short line at the end to print out the filename just to bookend the script.
I’ve been opening the files in notepad and they haven’t been changing. This is discouraging and encouraging at the same time. On the one hand, I know my script is right, on the other hand, I must be doing something else wrong. I’ll try calling the file with the cat command and see if anything changes.
This is the result that I get. Am I doing something wrong with the operation of Powershell?

I can’t quite spot any error here, and looking at your screenshot it seems like you’re running it right, so here’s some things to try:

  1. You may have the file locked or with the wrong permissions because it’s open in another program. Try a totally different random file name that can’t exist there and see if it’s the same error.
  2. Try deleting the code after the target.truncate() line and see if it even truncates the file. If it’s not doing that then that should give you a clue.
  3. Are you sure you’re really running this code? There’s a good chance that you had an error in your editor, made the changes but THOUGHT you saved it when you didn’t actually save it. Double check that you saved it, and to be sure try closing the file. If it’s not actually saved then it will warn you before you close it. You can also cat the file in the terminal and make sure your changes are there.

Try those.

1 Like
  1. The files are not set to be read-only or hidden, and my user accounts have all available permissions allotted to them, so it doesn’t appear to be a permissions problem.
  2. I tried this and found that the file is not being truncated.
  3. I am running this code. The code I posted was copy-pasted directly from my editor. And I’ve looked over it probably a dozen times.
    UPDATE! I called a file called ex21b.py instead of a .txt file and input python commands for the 3 input requests, and when I ran the ex21b.py file in powershell I got this result:
    When I open ex21b.py in my editor, however, none of the changes seem to have taken place, despite powershell having run the new code.
1 Like

I have no idea what’s going on then without actually watching you work.

You know, I think you’re close to solving this and what you should do is delete this code and rewrite it, but do it step by step and run it and make sure it’s working each time. Keep your old code around so you can look at it to see what’s going on.