0

I try to open and write something in file, but it gives that mistake: (me Operation not permitted: '/Users/archi/Desktop/Python/txt.pages)

I have tried to give permission to file, but all that doesn't work.

here is the code

r = open('/Users/archi/Desktop/Python/txt.pages')
r.write('something')
r.close

`

  • 1
    Is that the entire error message? If not, be sure to include the whole thing. – Code-Apprentice Jun 01 '22 at 02:51
  • 4
    1, do you have permission? 2. you need to specify that you are writing `open('filepath', 'w')` – drum Jun 01 '22 at 02:52
  • Most likely a permissions issue with the terminal where you ran the python script. – Jaye Renzo Montejo Jun 01 '22 at 02:54
  • if that is your code, then you need to [open](https://docs.python.org/3/library/functions.html#open) your fine in write mode, the default is opening for just reading. You are also missing the () in the close method – Copperfield Jun 01 '22 at 03:03
  • On recent versions of MacOS, there is additional protection for some directories, where you separately have to grant Python permission before it can write to your home directory. In this scenario, the system should throw up an error dialog, though. Perhaps see also https://stackoverflow.com/questions/17693408/enable-access-for-assistive-devices-programmatically-on-10-9 – tripleee Jun 01 '22 at 03:31
  • is `/Users/archi` your home directory? Suggest you use '~/Desktop/Python/txt.pages'. It's easier and safer. Also, the desktop probably isn't the best place to store your output files because it will clutter up your desktop visually. Suggest `~/Documents` or create your own directory for storing files using `mkdir` or the Finder. – Nic Jun 01 '22 at 05:34
  • 1
    once you get past the file access error, you will realize that **you cannot edit the file data in this format** you must decompress it from a `pages` file , edit it, then recompress it. an alternative is to use AppleScript as part of macOS's OSA (Open Scripting Architecture) – dbakr Jun 02 '22 at 15:34

2 Answers2

1

try this:

with open('/Users/archi/Desktop/Python/txt.pages', 'w') as r:
    r.write('something')

although i'm not sure how much help it's going to be as you are opening a .pages file which is encoded differently than a standard text file -- it contains a compression with styling information, text, etc.

dbakr
  • 217
  • 1
  • 10
-1

Are you missing the second parameter for .open()? The default is 'r' which does not allow you to write to the file, in my understanding.

From https://docs.python.org/3/library/functions.html#open:

Options for .open()

The default mode is 'r' (open for reading text, a synonym of 'rt'). Modes 'w+' and 'w+b' open and truncate the file. Modes 'r+' and 'r+b' open the file with no truncation.

Elmstead
  • 64
  • 9