EZ Sheets Python credentials-sheets.json - python

Does the credentials-sheets.json file and two tokens for drive and sheets need to be in the same folder as my python script, or can they reside somewhere else on my machine.
For example, I've got a test.py file on my desktop and ezsheets works correctly if the json file and two tokens are also on the desktop.
Do I need to copy/paste these files into the same folder as my python project, or is there somewhere else I can store them where ezsheets will find them? It seems kind of redundant to have to copy paste them every time, but I'm currently in my first coding class so perhaps I don't know best practices.

It's ideal if you can have the JSON file in the same folder, but not required. I'm unfamiliar with ezsheets, but I think it will work anywhere if you specify the correct file type. You should put the JSON file and the two tokens in the same folder (but not required).
For adding the file in the same folder, you can make use with the sys module.
import sys
path = sys.argv[0]
The path variable will have the script path. You can use this knowledge to determine the other files' path.

Related

Is there a way to load images without using the root file in pygame?

When I load images using pygame, I normally just use file locations such as: 'B:\Python\Images\image.png' but if I want this to be able to run on different computers without needing to edit the code every time, I would just want to go like to do 'image.png' instead. Is there a way to do this? Whenever I try to skip the B:\Python\' part, I get the error: "File Error: No file "image.png" found in working directory 'C:\WINDOWS\System32."
I've seen people use a file location such as: '../Images/image.png' instead but that didn't work (I may have done it wrong).
You have to set the working directory. The resource (image, font, sound, etc.) file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python script. The name and path of the python file can be retrieved with __file__. The current working directory can be set with os.chdir(path).
Put the following at the beginning of your main script to set the working directory to the same as the script's directory. This allows you to specify file paths relative to your main file:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

PyInstaller & NSIS Zip2Exe - Referencing files outside of your PC

I created an application that references multiple shapefiles. Example below:
df = gpd.GeoDataFrame.from_file(r'C:\PATH_ON_OWN_PC\FILE_NAME.shp')
As you can see from the above, the program references files from my own PC.
I then used NSIS to create a setup.exe file so the program can be downloaded and used with other PCs. I included all the datasets in the zip file.
The issue is when the application runs the script it's still referencing the path on my own PC, which obviously won't work.
Is there a way to alter the code so it reads the files from the downloadable .exe file, so it reads the files no matter the PC it's downloaded from.
Thanks!
You should be able to use something like this so that your .exe will work on other computers. Here is a generic example that points to a file inside of my Scripts dir, where my python.exe is located, for one of my Conda envs. You can replace the second and third params in os.path.join() to match up with the path of your shapefiles.
import os
import sys
python_exe_dir = os.path.dirname(sys.executable)
sample_file_path = os.path.join(python_exe_dir, 'Scripts', 'gdal2tiles.py')
print(sample_file_path)
'C:\\Users\\matth\\anaconda3\\envs\\gpd_0\\Scripts\\gdal2tiles.py'
So in your case, you'd end up with something like this.
df = gpd.GeoDataFrame.from_file(sample_file_path)

Where does "/folder1/folder2/file.ext" save to in Python?

I know there must be a really obvious answer, but it eludes me. Where does "/folder1/folder2/file.ext" save to in Python? In some code I thought (incorrectly) that it would save the file to a relative path, but when I checked folder2 it wasn't there. However, it seemed to still take up the hard disk space. I did a search on my hard drive and couldn't find the file. Where did it go? Again, I'm aware that there must be a really obvious answer out there, but I am just learning to code.
Firstly I would read about the os module and the module search path.
I opened the IDLE 3.7.0 on my Windows 10 machine and did the following:
>>> import os
>>> os.getcwd()
'C:\\Python37'
>>> os.path.abspath('./folder1')
'C:\\Python37\\folder1'
Windows expands the . to represent the current working drive, thus wherever the interpreter is looking is where your folders will reside.
In the links, it talks about how sys.path works (in short the directory containing the current script is placed at the front of it [e.g. the current working directory] for searching).
So, in short, either run your script directly from the directory or call os.chdir('/your/desired/path').

How to find path to current .py file in Spyder (Anaconda)?

Set-up
I run a script on my computer, located in the directory Users/path/to/my/script.py.
In the script, I use the path to the script, e.g.,
sub_path = 'Users/path/to/my/'
os.chdir(sub_path + 'other_script/')
As you can see, I define sub_path in the code 'manually'.
Problem
I don't want to define the sub_path manually, I'd rather have Python do it for me.
I'm looking for something similar to the code I use to obtain the current working directory: os.getcwd(), but then a code to obtain the directory of the current file.
I mainly find answers similar to this one, which says,
os.path.abspath(os.path.dirname(__file__))
but in the Spyder & Anaconda set-up, this generates a NameError: name '__file__' is not defined.
What can I do?
You if you want to move back one folder/directory you use the .. in your file path.
os.chdir('../other_scripts/')
will work. You may fine it helpful to view this or the wiki.
If you want to move from where you currently are you can use './new_dir/'. If you want to automate how to find other files you may want to read here which says to use os.walk. This may be the same question.
Mark8888 pointed out to run the whole script (run file (F5)) instead of just pieces of the script
this way multiple approaches should work to get the script file location and change the current working directory
import os
# directory of script file
print(os.path.abspath(os.path.dirname(__file__)))
# change current working directory
os.chdir(os.path.abspath(os.path.dirname(__file__)))
# current working directory
print(os.getcwd())
also
import os
import sys
# directory of script file
print(os.path.abspath(os.path.dirname(sys.argv[0])))
# change current working directory
os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
# current working directory
print(os.getcwd())
I add the following lines to any script I run in case I need to access data relative to the location of the script
import sys
script = sys.argv[0]
print(script)
'C:/SomeFolder/A_Subfolder/CurrentlyRunningScript.py' # changed obviously
First, save your Jupyter Notebook. Second, locate the directory your Jupyter Notebook is stored in. Thirdly, ensure that your Jupyter Notebook and CSV file are in the same place.

Specify a folder to save screenshots to in Desktopmagic

I'm using a module that saves screen shots into my Python34 folder as .png files.
I want the .png files to be relocated into a separate folder within the Python34 folder.
I want this to be done automatically. I was thinking maybe I could loop through the Python34 folder to find all the .png files and then save these files into a new "PNG" folder within the Python34 folder, but I don't know how I would do this. How should I approach this problem?
Some clarifications:
The module I am using is called "Desktopmagic", ( https://pypi.python.org/pypi/Desktopmagic/13.3.29 )
I am working with Windwows.
As I can see from the code of Lib\site-packages\desktopmagic\screengrab_win32.py:getDCAndBitMap (called from saveScreenToBmp), the module uses the provided file name argument as is.
I.e. if you specify it without a path, it saves into the current directory. So, if you wish to save elsewhere, either change it or specify a path (relative or absolute) in the argument. Btw, saving random files under the python installation dir is a very bad idea.
If you want to prepend a path by default, you'll need to use a wrapper or replace some of the module's machinery.
You can search a directory for a certain set of files using glob, for example
import glob
screenshots = glob.glob('*.png')
You can then copy the files by looping over the screenshots list and using shutil.copyfile

Categories

Resources