Reputation: 51
Documents = ['*pdf', '*docx', '*txt']
for i in range(len(Documents)):
if glob.glob(Documents[i]):
print(Documents[i], True)
shutil.move(glob.glob(Documents[i])[0], '/home')
else:
print(Documents[i], False)
Well, everything goes great until:
shutil.move(glob.glob(Documents[i])[0], '/home')
Which is basically:
shutil.move(scr, dst)
And produces the error:
*pdf False
*docx True
Traceback (most recent call last):
File "/usr/lib/python3.2/shutil.py", line 326, in move
os.rename(src, real_dst)
OSError: [Errno 13] Permission denied
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "teste.py", line 19, in <module>
shutil.move(glob.glob(Documents[i])[0], '/home')
File "/usr/lib/python3.2/shutil.py", line 334, in move
copy2(src, real_dst)
File "/usr/lib/python3.2/shutil.py", line 146, in copy2
copyfile(src, dst)
File "/usr/lib/python3.2/shutil.py", line 99, in copyfile
with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: '/home/jesus.docx'
I tried using absolute path in both scr and dst and it didn't work. I browsed the web and found out that it might have something to do with permissions but if i have to change the permissions it will defeat the purpose of the script and that's why i will try to find help here before getting into the permissions thing.
So, what do i do?
My purpose is to move files in Linux user's directories. (sort them according to the file format..)...ex-> .jpg --> Pictures, .pdf --> Documents and so on. (That's why i can't be having permission thing hardening life...)
Also i'm a newbie at programming (just so you guys don't get very geeky :D ) And, this is also my first questions here at this community so please little patience if i sound lost or retundant? Thanks
Upvotes: 4
Views: 6668
Reputation: 12077
You're attempting to write files to the /home
directory. You should not be doing that.
Linux is a multiuser operating system. /home
is where all of your users home directories live. Each user should have their own directory under /home
. For example yours could be /home/Joao
.
Change the last parameter from this line
shutil.move(glob.glob(Documents[i])[0], '/home')
to
shutil.move(glob.glob(Documents[i])[0], '/home/your_username')
Here's a general improvement suggestion:
# Variable names are lower case
documents = ["*.pdf", "*.docx", "*.txt"]
# You iterate over a for loop like this. Python's for is like for-each. You get the actual item.
for doc in documents:
if glob.glob(doc):
shutil.move(glob.glob(doc)[0], '/home/your_username/Documents')
Upvotes: 4
Reputation: 15501
You want users that run your script to be able to run it with elevated privileges. There is something called the setuid bit (set user id), which, when set, will run the script with the priveleges of the owner. There is also a setgid (set group id) bit.
To set them, become root and execute:
chown root scriptname
chmod ug+s scriptname
This sets both the setuid bit and the setgid bit.
http://en.wikipedia.org/wiki/Setuid
Upvotes: 1