Ex16 & Ex17 - Code stops working when it gets to target = open(filename, 'w') and out_file = open(to_file, 'w') for ex16 and 17 respectively

Hi there,

I’m quite new in Python. Kindly help me with exercises 16 and 17. For exercise 16, lines 1 - 8 runs smoothly, thereafter everything falls apart. Please see below:

PS C:\Users\code> python ex16.py test.txt
We’re going to erase test.txt.
If you don’t want that, hit CTRL-C (^C).
If you do want that, hit RETURN.
?

After pressing ‘enter’ I get the following problem.

Opening the file…
Traceback (most recent call last):
File “ex16.py”, line 12, in
target = open(filename, ‘w’)
PermissionError: [Errno 13] Permission denied: ‘test.txt’
PS C:\Users\code>

For exercise 17, lines 1 - 16 runs perfectly, and then it’s the same problem afterwards. Please see below:

PS C:\Users\code> python ex17.py test.txt new_file.txt
Copying from test.txt to new_file.txt
The input file is 68 bytes long
Ready, hit RETURN to continue, CTRL-C to abort.

I get the following after pressing ‘enter’

Traceback (most recent call last):
File “ex17.py”, line 16, in
out_file = open(to_file, ‘w’)
PermissionError: [Errno 13] Permission denied: ‘new_file.txt’
PS C:\Users\code>

Please note that I’m using Python 3.8, and Windows PowerShell.

Hmm. Are those existing files or are they created by the script? It looks like Windows is not allowing Python to access them. Can you check which permissions are set for them?

(The permissions are shown somewhere in the file properties dialog. Powershell should be able to tell, too, when you list them, but OTOH I don’t remember how to read that output.)

1 Like

Thanks, Florian. The problem was indeed with permissions. It’s now fixed.