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)
Related
Hi I get this when running python script(prepoc.py). Could it be an issue with the environment variables?
Any idea how to fix this? Thanks
Edit: it's in the directory
Please open the path program and see your program is there. This error means your program is not there.
First of all, check if the prepoc.py is in your folder directory. If it already is, you have to check your current working directory by
import os
print(os.getcwd())
if prepoc.py isn't in the current working directory, place it there.
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)
Recently I've been working on a Python project that requires human interaction with other files. I compiled it to one file. I tried to get the absolute path to that compiled file. However, everything the internet has suggested leads me to the _Meixxxx temp folder. Although useful, it's not very well situated for human interaction. Is there any way that I can find the absolute path to my exe file and not the _Meixxxx folder?
I've tried several things including:
os.cwdir
os.path.dirname
Thank you for any help in advance
EDIT:
Thanks to #johnashu
os.path.dirname(os.path.realpath(__file__))
This seems to correctly find the path of the file location
As #johnashu suggested
os.path.dirname(os.path.realpath(__file__))
should give you the actual location for your exe file
Im trying to navigate directories in python like this, and for some reason its telling me that
IOError: File ../Harvard%20Stats%20Course%20Data%20Files/diamonds.csv does not exist. The file does exist, however, one folder out, and then in the Harvard Stats Course Data Files folder.
diamond_data = pd.read_csv('../Harvard Stats Course Data Files/diamonds.csv',
sep = ',', index_col=0)`
Any advice would be appreciated.
At the IPython prompt, type pwd. That will show you the current working directory. Perhaps it is not the directory you think it is. You can change the current workding directory by typing cd /path/to/dir at the IPython prompt.
Alternatively, you could simply supply an absolute path to the CSV file.
Using ../ like that will be relative to wherever you're running the program from. Try to change into the directory of the .py file and run it again.
I'm new and I have no idea where the default directory for the open() function is.
For example open('whereisthisdirectory.txt','r')
Can someone advise me? I've tried googling it (and looking on stackoverflow) and even putting a random txt file in so many folders but I still can't figure it out. Since I'm beginning, I want to learn immediately rather than type "c:/directory/whatevevr.txt" every time I want to open a file. Thanks!
Ps my python directory has been installed to C:\Python32 and I'm using 3.2
os.getcwd()
Shows the current working directory, that's what open uses for for relative paths.
You can change it with os.chdir.
If you working on Windows OS first type
import os
then type
os.getcwd()
and it should print the current working directory.
The answer is not python-specific. As with programs written in any other language, the default directory is whatever your operating system considers the current working directory. If you start your program from a command prompt window, the CWD will be whatever directory you were in when you ran the program. If you start it from a Windows menu or desktop icon, the CWD is usually defined alongside the program's path when creating the icon, or else falls back to some directory that Windows uses in the absence of that information.
In any case, your program can query the current working directory by calling os.getcwd().
The default location is the CWD (Current Working Directory), so if you have your Python script in c:\directory and run it from there, if you call open() it will attempt to open the file specified in that location.
First, you must import:
import os
Then to print the current working directory:
os.getcwd()
If you want to change the current working directory:
os.chdir('your_complete_required_path')
create the .txt file in the directory where u have kept .py file(CWD) and run the .py file.
The open() function for file always creates files in the current working directory. The best way to find out your current working directory is to find three lines of small code:
import os
current_working_directory = os.getcwd()
print(current_working_directory)
Run this above code and you will get your current working directory where open() function creates a new file. Good Luck!
If you’re running your script through an interpreter (i.e pycharm, VSCode etc) your Python file will be saved, most likely, in my documents (at least in VSCode, in my personal experience) unless you manually save it to a directory of your choosing before you run it. Once it is saved, the interpreter will then use that as you current directory so any saves your Python script will create will also automatically go there unless you state otherwise.
it depends on how you run it from the terminal
like this, it is going to look in your home directory
C:\Users\name>python path\file.py
and like this, it is going to look next to your file
C:\Users\name>cd path
C:\Users\name\path>python file.py