find path to an application using a python application - python

I am currently learning Python and looking to build small apps just for practice. I am wanting to build a Windows application that will delete a folder in the C:\users\<username>\appdata\roaming folder. The problem I have is the username will be different depending on what workstation a person is on. I am looking for someone to lead me in the right direction on how I would go about finding this path on each workstation and then deleting a folder within that path. I have looked at relative paths but not sure if I would be able to use that to delete a folder within the %appdata% folder.

Tell me if I got it wrong. Are you stuck trying to get the username dynamically when run from each account? If so, you can try python's getpass module as below,
import getpass
location = "C:\users\{username}\appdata\roaming".format(username=getpass.getuser())

You can use os.getlogin() to get the current user and insert it as a placeholder:
import os
currentUser=os.getlogin()
folderPath="C:\\{0}\\<username>\\appdata\\roaming".format(currentUser)
os.rmdir(folderPath) ----> removes an empty directory.
shutil.rmtree(folderPath) ----> deletes a directory and all its contents.

You can try this to get the appdata path
import os
path = os.getenv('APPDATA')
I found it here.
And after, to delete the folder (in this case I deleted the Zoom folder)
import shutil
folder_to_delete = os.path.join(path,'Zoom')
shutil.rmtree(folder_to_delete, ignore_errors=True)

Related

Can't import when Path has spaces

Python3.7 on OS X 10.14.5
I have a python script that imports a single local module. They are in the same directory, and everything was working fine. It's a pretty simple setup.
I moved everything over to another directory (on the same machine) and local imports stopped working. The only thing I can tell that has changed is the path, which now contains a folder called "My Drive". I believe the spaces in the path are causing the problem with the import.
I've searched the web, and tried many things, but can't seem to get import to work with a folder in the path that contains spaces. Anyone know any solution to this or a resource to try to figure it out?
I cannot change the folder name.
EDIT:
This was the directory structure:
main.py
lab/
--labs.py
I was able to get it working by flattening out the directory structure. However, when using relative imports:
import lab.labs
or adding 'lab' to the path:
os.path.append("./lab")
import did not work in that location.
Symlinking also works, but that seems like running around python, rather than fixing it within python, and since this is code that will be shared, I don't want a single machine solution.

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.

How do I set the search path for a Python import?

I'm using Windows 10 with VS2015 and Python 3.4. I have created a module called this.py. Now I want to import this.py. That works OK if this.py is in the current folder. But I need this.py to be in a different folder which is used by all of my scripts.
I have tried a few approaches but none would find this.py. One approach was to set my Windows CMD PATH.
Can someone tell me how to set the search path for imports?
You should do something like this:
import sys
sys.path.append("path-here")
Then that directory will be searched when you import something (other than standard directories)
Edit: the path of the directory containing the imported file, not of the file

What are the file places after you package a python program?

I am wanting to package my program that uses over files to store user data locally, but I don't know what directory I should put in all the json.load and json.dump. So right now, I have the directory equal to json.dump(somelist,open('/home/username/filename','w')) but when someone downloads it, the program won't work since it is a different directory. I am trying to PyInstaller but maybe PyInstaller will do it for me. I was just wondering and I couldn't find anything on google but if there is something, please link it to me. Thanks in advance!!
Use the following to get the user's home directory:
from os.path import expanduser
home = expanduser("~")
with open(os.path.join(home, 'file'), 'w') as sr:
json.dump(somelist, sr)

How can I specify a path on a Mac

I'm using Python 2.7.2. and I want to open and use a dictionary I created in my shell. My problem is, when I try to import this dictionary into my shell it can't find the file because python is just looking into the 'my documents' folder.
My question is, how can I navigate to the correct folder (just one folder further in 'my documents' folder.
I am using a Macintosh.
You can add custom path to your script as:
import sys
sys.path.append('/Users/username/')
Another way is to set the PYTHONPATH environment variable to /User/username in your shell. Since you know about your shell, I expect that you already know how to edit your shell resource script. You could also add it to your .profile file in which case it should be available even if you change which shell you happen to be using.
If you don't want to "hardwire" the system path, but want to use a relative folder, you could get the name of the working directory and add your subdirectory to that...
subdirname = 'myfolder'
curdir = os.getcwd()
newdir = os.path.join(curdir,subdirname)
sys.path.append(newdir)

Categories

Resources