1

I want to use the following code to change the owner of files:

import os

from getpwnam import pwd
from getgrnam import grp

uid = getpwnam('edamame')[2]
gid = grp.getgrnam('staff')[2]
os.chown('/Users/edamame/workspace/git/chinese_nlp/venv/lib/python3.7/site-packages/psutil/_psosx.py', uid, gid)

But the following errors:

Traceback (most recent call last):
  File "/Users/edamame/workspace/git/chinese_nlp/chinese_segmenter1.py", line 6, in <module>
    from getpwnam import pwd
ModuleNotFoundError: No module named 'getpwnam'

Process finished with exit code 1

I am using Python 3.7 virtual env in PyCharm. I couldn't find the module called getpwnam to install. Which package should I install? Thanks!

Edamame
  • 23,718
  • 73
  • 186
  • 320

1 Answers1

1

You got the imports a bit backwards, I know, it happens :) Try this (pwd and grp are just standard Python library modules):

>>> from pwd import getpwnam
>>> getpwnam('root')[2]
0
>>> from grp import getgrnam
>>> getgrnam('root')[2]
0
Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97