Denied permissions to download file from Dropbox API in Python - python

I'm trying to download a CSV file from Dropbox using their library in Python, but I'm having quite the issue trying to get the permissions needed to be able to actually get it into the computer.
The error message that I'm getting in any case is this:
PermissionError: [Errno 13] Permission denied: 'my_directory'
So I've tried the only two solutions I'm finding as I investigate: 1) change the mode of the directory to '0o777' using os.chmod(), and 2) running things as an admin. But even with both, I'm still not getting rights to write the file to my local computer. It might also be worth noting that the name of the file and the directory I'm trying to write it to do not have the same name.
import os
import dropbox
local_dir = 'absolute_path_to_directory'
os.chmod(local_dir, 0o777)
dbx = dropbox.Dropbox('my_token_here')
dbx.files_download_to_file(local_dir, 'path_to_file_in_dropbox')
I would think that running this kind of program as an administrator and having changed the directory permissions, I would have permissions to actually download the file to it. But I'm still getting the PermissionError I noted above.
Any help or insight into what might be causing the issue would be much appreciated.

According to the documentation chmod you can only set the read-only flag all other bits are ignored. However, this might not be the problem.
Looking at the dropbox forum, it looks like you might be missing a '/' along with the filename, and not just the directory.
dbx.files_download_to_file(j, '/'+ j)
According to Greg
You do need to use the full path when specifying the file you want to download. Using files_list_folder and files_list_folder_continue will give you every entry, and you can get the full path from the returned (File)Metadata.path_lower. (I.e., use entry.path_lower instead of '/'+entry.name.)

Related

Pandas unable to read my file despite correct path

Before anyone says "there are already solutions for this" or "check the path, relative or absolute", I have already done all of that. I am working on my final uni project and this is driving me nuts.
I'm trying to import some molecules using pandas:
df_chlorophenol = pd.read_csv(r'Neural_Network_V1/Simulation_Dataset/Chlorophenol.csv', index_col=False)
The path for the .csv file is:
Simulation_Dataset/Chlorophenol.csv
And my main file is, of course, in Neural_Network_V1. I keep
getting "No such file or directory" and it is driving me crazy because I've tried everything and it won't change. The funniest part is that it worked yesterday.
Any help would be appreciated.
This file has been working perfectly, no changes on my code, and suddenly it decided to stop working, I've made a new file and still, pandas can't read any .csv
If the script is in Neural_Network_V1, and you use the path Neural_Network_V1/Simulation_Dataset/Chlorophenol.csv, then it is a relative path, and the script will look for that path relative to where it is run from, so effectively it’ll try Neural_Network_V1/ Neural_Network_V1/Simulation_Dataset/Chlorophenol.csv.
A few things I would try:
Give an absolute path, regardless of where your application or the file is. For example C:/something/anything/Neural_Network_V1/Simulation_Dataset/Chlorophenol.csv on Windows, or /something/anything/Neural_Network_V1/Simulation_Dataset/Chlorophenol.csv on Linux.
Check the permissions on the file if you are on Linux.
Run the applicaton as Administrator on Windows. (It shouldn’t be necessary, but worth a try if the path is correct and it doesn’t work.)
You can also try copying the file into the same path where the script is, and just use the filename without any path information.

How to change VS code working directory to the running file directory

I've been trying to set the default working directory in VS Code, when a workspace is not open, to the directory where the file being executed is.
This is the normal behaviour in other IDEs, like Python IDlE.
I need this so my students can run a program from whatever folder they have it in, and it can open files referred to by their program using relative reference. They always have the file in the same directory as the running file (for example an MP3 that they want to open during their program).
I've read for hours lots of documentation, both in VS Code and Stackoverflow, without success.
I know that setting a workspace to that folder will solve it, but is not a viable solution for us, as they will be opening files from different locations all of the time.
I've tried changing the terminal.integrated.cwd in the settings.json file to take by default the directory where the file being executed is (like in IDlE), without success. I can't find the string that I need to include in terminal.integrated.cwd for this.
I've tried the following strings ".", ".", ".\", ""
I've also tried this:
"terminal.integrated.cwd": "${fileDirname}"
But when I run the following piece of code to see if the working directory has changes, after reset of VS code:
import os
cwd = os.getcwd()
print("Current working directory: {0}".format(cwd))
It still shows me the working directory as c:\users\my_user_name
And not the one where the file running this code is.
Could someone please tell me what can I do next? Thank you.
Murphy's law, 5 minutes after posting the question (and after hours of research) I come across this post with a solution that works perfectly.
Settings--> Python>Terminal>Execute In File Dir
I think, os.chdir(path) can be a solution in your case.
https://docs.python.org/3/library/os.html#os.chdir

Cannot get Perforce Triggers to work properly

Been trying to get this working all day, and i just can't figure out why its not working.
Trying to implement a simple trigger to run when the user submitted a file.
example in the .tmp file:
hello_trigger change-submit //testDepot/... "python F:/triggers/hello_trigger.py"
when i try to submit a file i get this:
Submit validation failed -- fix problems then use 'p4 submit -c 10199'.
'hello_trigger' validation failed: python: can't open file 'F:/triggers/hello_trigger.py': [Errno 2] No such file or directory
File exists and can be read, so its not a python issue.. same error with a .txt or .bat file.
From what i can gather the problem seems to be coming from the depot line in the trigger.
//testDepot/... fails
//depot/... doesnt fail, but the script is never fired off.
Any suggestions are greatly appreciated.
also testDepot is a stream not sure if that matters.
python: can't open file 'F:/triggers/hello_trigger.py': [Errno 2] No such file or directory
seems pretty clear that the file doesn't exist, at least from the point of view of this trigger command. Some things to double check:
This is running on the server machine, i.e. the place where the p4d service is running. If you have the script on your client machine the Python executable on the server isn't going to be able to find it!
Similarly, this is being run by whatever user is running p4d (on Windows this is usually the SYSTEM user, which may have limited permissions). Does that user have permission to read this path?
Could it be that your version of Python on Windows doesn't know how to handle paths with Unix-style forward slashes? (Many tools will normalize these for you but you shouldn't depend on it!) Try using a valid Windows path, i.e. F:\triggers\hello_trigger.py.

Hidden .db files using Python and SQLite3

I am using sqlite3 and python for a new website I am creating. The problem is, the "files_storage.db" file I am trying to create will not appear in any Windows 10 Folder Window, PyCharm's Directory View, nor will it appear via the command line interface.
The catch to this is, if I execute my python script multiple times, I get an error that states the database file already exists... So this file is somewhere, I guess it is a game of cat and mouse to find it.
I have ran into this problem before, but I have ALWAYS been able to find the file via the command line. Usually, I wouldn't both yall with such a question but, this is really irking me and I am going to run into serious issues when it comes time to put this baby on a server. :(
thanks in advance, and here's some screenshots I suppose.
You are using a relative file path.
Relative paths are converted to absolute ones by something like os.path.join(os.getcwd(), <relative path>). So they depend on the current working directory of the process.
Try to open it with an absolute path (starting with drive letter) to avoid any ambiguities.
If you use just a filename without a path, the file will be saved in whatever the current working directory of the Python interpreter is.
To see where the current working directory is, add the following code to the beginning of your program:
import os
print(os.getcwd())
You should then see the working directory in the output.
There is a setting for the current working directory in your IDE somewhere. See e.g. the answers to this question.
You can also do something like:
import os
path = os.path.expanduser("~") + '/Documents'
print(path)
This will allow you to access the directories for the current user. For me, this prints:
'/Users/thomasweeks/Documents'

How do I search the 'Startup' folder in Windows and delete files I don't want?

Basically, what I'm trying to do is make a program that once run, saves a copy of itself to the Windows 'Startup' folder.
My friend however has made some sort of 'antivirus' (it's not that great) that checks 'Startup' - when Windows starts up - for any files with the extension .py or .pyw.
I'm trying to make a section of my program that does the same sort of thing, only it's supposed to delete his file before it has time to delete mine.
The program I'm editing is not originally mine, therefore I don't really understand a lot of it, but I have an alright knowledge about Python. I've tried this in the place I think it's supposed to go, but I get an error:
for file in os.listdir("C:/Users/%s/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/"):
if file.endswith(".pyw") or file.endswith(".py"):
os.remove(file)
This is the error I get when I have a file (Test.py) in the 'Startup' folder:
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Test.py'
Any suggestions would be appreciated. Thanks.
Change os.remove(file) to os.remove("C:/Users/%s/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/" + file

Categories

Resources