root olarak çalıştırmak demektir. Yani normal dosya dosyanız root'a ait.
Yapabilecekleriniz, oluşturulduktan sonra dosyanın sahipliğini değiştirmek. Bunu yapmak için hangi kullanıcının sudo çalıştırdığını bilmeniz gerekir. Neyse ki, sudo kullandığınızda ayarlanmış bir SUDO_UID
ortam değişkeni var.
Yani, bunu yapabilirsiniz:
import os
print(os.environ.get('SUDO_UID'))
Sonra change the file ownership gerekir:
os.chown("path/to/file", uid, gid)
Beraber koyarsanız: Elbette
import os
uid = int(os.environ.get('SUDO_UID'))
gid = int(os.environ.get('SUDO_GID'))
os.chown("path/to/file", uid, gid)
, sen olacak Bir işlev olarak istiyorum, çünkü daha kullanışlı, yani:
import os
def fix_ownership(path):
"""Change the owner of the file to SUDO_UID"""
uid = os.environ.get('SUDO_UID')
gid = os.environ.get('SUDO_GID')
if uid is not None:
os.chown(path, int(uid), int(gid))
def get_file(path, mode="a+"):
"""Create a file if it does not exists, fix ownership and return it open"""
# first, create the file and close it immediatly
open(path, 'a').close()
# then fix the ownership
fix_ownership(path)
# open the file and return it
return open(path, mode)
Kullanımı:
# If you just want to fix the ownership of a file without opening it
fix_ownership("myfile.txt")
# if you want to create a file with the correct rights
myfile = get_file(path)
DÜZENLEME: Robᵩ @, @Basilevs cevabım sayesinde Güncelleme ve @ oluşturduktan sonra
'it chown' 5gon12eder. –
Gerçek kullanıcı olarak açılmasını istiyorum, bunun kim olduğunu düşünmeden ... sabit kodlanmış bir şey değil – ABR
Geçerli kullanıcıyı belirleyebilir ve bunu seçebilirsin. –