Google Drive API Invalid Secrets File Through Task Scheduler - python

I need some help troubleshooting something that should be easy to figure out. I have the following files in this folder on my computer: C:\Users\theda\Documents\Python\CANSLIM Script
run_script.bat
client_secrets.json
credentials.json
settings.yaml
These same files (except for the .bat file) plus my Python code are in this folder: C:\Users\theda\PycharmProjects\CANSLIM_Script
.bat file code:
cmd /k python C:\Users\theda\PycharmProjects\CANSLIM_Script\canslim_script.py
When I run my script by double-clicking on run_script.bat, it is able to find my client_secrets.json file and authenticate me with Google Drive. However, when I run the same .bat file through Windows Task Scheduler, I am getting the following Traceback:
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\oauth2client\clientsecrets.py", line 121, in _loadfile
with open(filename, 'r') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'client_secrets.json'
Where do I need to place my credentials/settings/etc. for PyDrive when running a Python script with a .bat file through Windows Task Scheduler? Or, how can I figure out where it is looking for the file?

I was able to get this to work by supplying a location to my auth and settings files using the following lines of code. Hope it helps someone:
secrets_file = os.path.normpath("C:/Users/theda/Documents/Python/CANSLIM Script/client_secrets.json")
settings_file = os.path.normpath("C:/Users/theda/Documents/Python/CANSLIM Script/settings.yaml")
gauth = GoogleAuth(settings_file=settings_file)
GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = secrets_file

Related

Error when running program through virtual machine

We have written a python script to run within a forced speech alignment program. The speech alignment program was written with python but is run as an image through a Virtual Machine. The script instructs the program to align all of the files in a designated folder that is shared between the Virtual Machine and the computer's hard drive.
The command line of the VM image points to the shared folder where the files and the python script are located. It successfully opens the python script but once we try to open the directory specified in the script it says that there is no such file found. Would this have anything to do with the fact that it is being run in the virtual machine?
Here is the part of the script that points to the directory:
import sys
import os
def main():
direct=r'/Desktop/Shared/sf_VM_Shared/faseAlign/F3'
for file in os.listdir(direct):
and here is the error message:
ubuntu#BCE:~/Desktop/Shared/sf_VM_Shared/faseAlign/F3$ python3 FASE.py
Traceback (most recent call last):
File "FASE.py", line 19, in <module>
main()
File "FASE.py", line 9, in main
for file in os.listdir(direct):
FileNotFoundError: [Errno 2] No such file or directory: '~/Desktop/Shared/sf_VM_Shared/faseAlign/F3'

How to run a Python script in VSCode using the same path of the script? (otherwise it can't find an input file located in the same folder)

I have this Python code: https://github.com/andreagrandi/aoc_2019/tree/master/aoc
which runs perfectly from the terminal (Python 3 required) if I do for example: python aoc_03.py
But if I try to run it from VSCode, taking advantage of the Python extension and integration, I get this error:
(aoc) ➜ advent_of_code_2019 git:(master) /Users/andrea/.virtualenvs/aoc/bin/python /Users/andrea/Documents/advent_of_code_2019/aoc/aoc_03.py
Traceback (most recent call last):
File "/Users/andrea/Documents/advent_of_code_2019/aoc/aoc_03.py", line 70, in <module>
with open('aoc_03_input.txt', 'r') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'aoc_03_input.txt'
My guess is that when invoked from VSCode, the script is run from a different path, so it cannot find the file aoc_03_input.txt which is located in the same folder of the script.
How do I tell VSCode to run my script from the /Users/andrea/Documents/advent_of_code_2019/aoc/ folder, so that it will be able to find my input file?
Thanks
Actually, I should have tried more before asking this question, because I just found the solution, but at this point I will write it here in case it can be useful to anyone:
If I change the path in this way:
with open('./aoc_03_input.txt', 'r') as file:
The file is being open correctly when I run the code in VSCode and when I run it from the terminal. Tested under OSX (but it should work under Linux too. Not sure about Windows).

Python open and "permission denied" on file with ugo+rw?

I have a script on a RHEL 7.x machine written in Python3. In testing this script I created a function which will append to a text file in the same directory.
If I execute the script from the local directory ie - ./pyscript.py everything works as expected.
But I am trying to execute this from a Bash script a couple directories higher and it doesn't seem to work right. The other functions in the script will execute, but this very last one which appends to a text file will not.
Now, if I run the script as the user which owns it(and the txt file) from my home dir, the script errors out with a permission error. BUT if I run the script with sudo it finishes with NO error, However it does NOT write to the text file.
My user has RW privileges on every dir between the bash script and the python script.
Any thoughts on why a sudo or local user run doesn't seem to let me write to the text file??
Edit
Traceback (most recent call last):
File "ace/ppod/my_venv/emergingThreats/et_pro_watchlists.py", line 165, in <module>
with open('etProLog.txt', 'a') as outlog:
PermissionError: [Errno 13] Permission denied: 'etProLog.txt'
If you use open("filename.txt", 'mode'), it will open that file in the directory from which the script is executed, not relative to the current directory of the script. If you want the path to directory where the script exists, import the os module and use open(os.path.dirname(os.path.abspath(__file__))+"filename.txt"). The permission error is because the file doesn't exist; sudo overrides that but does nothing because the file doesn't exist.

Python: How do I open the most recent file in my downloads directory?

I have a button in my UI that I am clicking which downloads a .csv file:
def download_log_events(self):
self.event_record_dropdown()
download_bttn = self.driver.find_element(*Elements.download_as_csv)
download_bttn.click()
The .csv file is saved as the most recent file in my downloads folder, and I need some advice on how to open this file using Python. Currently I have the following:
import os
def open_downloaded_file(self):
dir_path = os.path.join(os.path.expanduser("~"), "Downloads\\")
If you can provide any help on what I should do next or how to open a file downloaded to my local machine, it would be great!

Error with Reading Files

I am working through Learn Python the Hard Way but am having a problem with an exercise that reads a file: https://learnpythonthehardway.org/python3/ex15.html
I have followed this and made sure my file is in the same folder as my python file. I keep getting the error:
Traceback (most recent call last):
File "ex15.py", line 5, in <module>
txt = open(filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ex15_sample.txt'
I am entering: python ex15.py ex15_sample.txt into the powershell. I tried looking up the problem I'm having but could not find anything that helped in my case. Let me know if I am doing something wrong or if I'm missing something. Thanks
You can check your current path and whether your file exists by
import os
#get the current working directory
os.getcwd()
#check whether your file exists in the current working directory
os.path.exists("./ex15.py")
try 2 things below
Check both files .py and .txt are at same location. and then execute program.
this is how i ran the code.
C:\Users\db\Desktop\Python>python ex15.py
enter File Name: ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
If your file is at other location you need to provide absolute path for your file. as
C:\Users\db\Desktop\Python>python C:\Users\deepakb\Desktop\ex15_sample.txt
i had my txt file at desktop this time.
A brief demonstration on working directory and relative file locations..
Say we have a text file named hello.txt with the contents:
Hello World!
that is located at "C:\Users\username\Documents\python\hello.txt"
And we have a python script named script.py with the contents:
with open('hello.txt', 'r') as f:
print(f.read())
Which is located at "C:\Users\username\Documents\python\script.py"
If I just hit the windows key and type power shell and hit enter to open it, I'll see it by default gives me a prompt at my home directory: "C:\Users\username>". From here I can execute my python script by calling C:\Users\username> python C:\Users\username\Documents\python\script.py. If I do this, however, they python interpreter would have started in my home folder, and when it tries to find "hello.txt" it will only search through the folder "C:\Users\username". That's not where the file is, so we get a file not found error.
To resolve this we have a few options. Number one is to re-write our script file to include the full path of our text file: ... open("C:/Users/username/Documents/python/hello.txt", ... This way there is no searching necessary, and the script can be run from anywhere. Another option is to change the working directory of the interpreter from within the script. You can set the working directory with the os library by calling os.chdir('C:/Users/username/Documents/python'). Finally you can set the working directory with PowerShell before calling the python interpreter using the cd command:
C:\Users\username> cd .\Documents\python\
I had the same error.
It was a mistake from me naming the file as ex15_sample.txt.
When you are creating a file in notepad, the file name would be automatically .txt
so if you have created a file just rename it as ex15_sample.
or
You can also run the same program by giving python ex15.py ex15_sample.txt.txt
As you can see the file name might be ex15_sample.txt with a .txt extension.

Categories

Resources