I am automating a system to create users in python and run a python script through the user. Here is the code I am running along with the error. The root is creating a user and then running the file as root. The folder being copied from skel looks like this /etc/skel/newFolder. username variable is 'newUser'

os.system('sudo adduser --disabled-password --gecos "" ' + username)
path = '/home/'+username+'/newFolder' 
path.replace("'", r"\'")

print(subprocess.run("test -e '{}'".format(path), shell=True))
subprocess.run("sudo python3 '{}'".format(path), shell=True)


Adding user 'newUser' ...    
Adding new group 'newUser' (1000) ...    
Adding new user 'newUser' (1000) with group `newUser' ...   
Creating home directory `/home/newUser' ...    
Copying files from '/etc/skel' ...    
CompletedProcess(args="test -e '/home/newUser/newFolder'", returncode=1)    
python3: can't open file '/home/newUser/newFolder/pythonFile.py': [Errno 2] No such file or directory

The return code of 1 means the test said this 'newFolder' copied from skel doesn't exist. And of course python3 cant find the python file. If I go through the terminal and look for this directory, it does exist. Very frustrating please help.


The entire error I was getting was because of a capitalization error in the file path. YOU ARE JOKING RIGHT. Problem solved.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.