Python Forum
[SOLVED] [Linux] Write file and change owner?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [Linux] Write file and change owner?
#1
Hello,

In a script in Linux, I need to write a text file, and then change its owner. The script will run as a cron job.

The following script doesn't work, without triggering an error.

What's the right way to proceed?

Thank you.

#!/usr/bin/python3
"""
apt install python3-os
E: Unable to locate package python3-os
"""
import os

OUTPUTFILE = "/usr/share/nginx/html/blah.html"

f = open(OUTPUTFILE, 'w', encoding='utf-8')
#stuff here
f.write("blah")
f.close()

#-rw-r--r-- 1 root     root
os.system(f"chown www:www {OUTPUTFILE}")
Reply
#2
From w3schools
https://www.w3schools.com/python/ref_os_chown.asp
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
Thanks. The file is still owned by root:root :-/

"""
apt install python3-pwd
E: Unable to locate package python3-pwd
apt install python3-grp
E: Unable to locate package python3-grp
"""
import os
import pwd
import grp

uid = pwd.getpwnam("www").pw_uid
gid = grp.getgrnam("www").gr_gid
#print(f"uid:{uid}, gid:{gid}") #OK
os.chown(OUTPUTFILE, uid, gid)
Reply
#4
This seems to work. Got it from https://stackoverflow.com/questions/3949...-in-python
import os
import sys

if os.geteuid() != 0:
	os.execvp('sudo', ['sudo', 'python3'] + sys.argv)
	
os.chown('test.txt', 1000,1000)
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#5
Thanks, it does work: -rw-r--r--

Alternatively, is there a simple way to run a Python script as non-root (manually in the console or through cron) so that files it creates are owned by that non-root user?

--
Edit:
root@debian:~# sudo -u www ./script.py
sudo: unable to execute ./script.py: Permission denied
Reply
#6
Are you wanting the script executed by the www user?
Could you not login as that user or root then set the permissions for www to execute the script?
Set the crontab for that user to execute the script?
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#7
Ultimately, the script should run through cron, which can indeed run a script as a non-root user. Here, I was experimenting to see how to change a file's owner in Python.

Turns out you only need this to change file ownership:
from pathlib import Path
import shutil

OUTPUTFILE = "/tmp/test"
Path(OUTPUTFILE).touch()
shutil.chown(OUTPUTFILE, "www-data", "www-data")
Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 28,119 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  How to write variable in a python file then import it in another python file? tatahuft 4 953 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Can a windows file be 'remapped' in Linux ? jehoshua 3 896 Dec-07-2024, 03:33 AM
Last Post: jehoshua
  [SOLVED] [Linux] Run Python script through cron? Winfried 2 1,227 Oct-19-2024, 06:29 PM
Last Post: Winfried
  [SOLVED] Reportlab - change a single page orientation beetlecad 1 1,504 Aug-16-2024, 05:43 PM
Last Post: beetlecad
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 1,817 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
Bug Copying methods to effect the new owner instead of the old instance Daniel285 2 988 Jun-03-2024, 07:58 AM
Last Post: Gribouillis
  What does .flush do? How can I change this to write to the file? Pedroski55 3 1,356 Apr-22-2024, 01:15 PM
Last Post: snippsat
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 10,113 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Last record in file doesn't write to newline gonksoup 3 1,613 Jan-22-2024, 12:56 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020