This question already has answers here:
How to find the real user home directory using python?
(9 answers)
Closed 1 year ago.
I finished up my first GUI project that allows the user to open any PDF file by wither typing a product key, or manually find it using a tree view. I have all the files in the same folder as the application, the problem is that when I used pyinstaller to make it an .exe it wont pull up. and anytime I move it nothing pulls up because the code is linked to that specific location. my question is how do you write it to where the filepath is linked to a specific folder, and even if said folder moves, it still accesses those files?
def Product_Look_UP():
if inPro.get()== "24LPPS":
Label(root, text= f"Searching for {inPro.get()}..." , fg = "Orange").grid(row = 3 , column = 0)
import webbrowser as wb
wb.open_new(r'C:\Users\user\Desktop\Product Finder\Data\Vender\vender\Ladder Pulls\24 in\Polished Stainless\LADDER PULL.pdf')
Essentially how do make "C:\Users\user\Desktop" be where ever the "Product Finder" folder is, whether it be on my computer, the server, or another persons computer?
You can call the function os.path.abspath and pass it the file name of your main python script and it will return the path where the file is. You can do something like this:
import os
import webbrowser as wb
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
wb.open_new(f"{CURRENT_DIR}/data/test.pdf")
Related
This question already has answers here:
Quick and easy file dialog in Python?
(8 answers)
Closed 3 days ago.
I want a user to be able to choose where to save a file through one of these windows, but I don't know how.
So far all I have for downloading is
dst_folder = input("Enter in the folder you want to download this file to: ")
stream.download(dst_folder)
This works, but you can type in literally anything for the input.
Please try this method
from tkinter import filedialog
from tkinter import *
root = Tk()
root.withdraw()
# Ask the user to select a folder
folder_selected = filedialog.askdirectory()
# Use the selected folder to download the file
stream.download(folder_selected)
This question already has answers here:
Open file in a relative location in Python
(14 answers)
Closed 7 months ago.
I'm new to coding so I followed a tutorial for a chatbot, I need to import my intentions (which I named intentii, in my language) intentii.json into the chatbot, so I'm using:
intentii = json.loads(open('intentii.json').read())
I have seen other questions about the error I said in the title, and yes, I made sure the name is typed right, it's in the same exact directory as all my files including this chatbot one are, and still, it says it can't find the file, I tried placing the whole directory path and it seemed to work ( I didn't get the same error again, but an unicode error which I believe is another problem related to my .json file ), but I cannot use the whole path because I have to send this to my teacher and, of course, he won't have the same exact path as mine.
What is the solution?
Edit: Also, I've noticed that it says that for every file I need to access from the folder
Try this function to load a json file:
import json
def load_json(filepath):
with open(filepath) as f:
data = json.load(f)
return data
Then
intentii = load_json("intentii.json")
Try using this python os module to find dir_root where current file is located and then combine it json file name if it is stored in same directory.
import json
import os
dir_root = os.path.dirname(os.path.abspath(__file__))
intentii1 = json.loads(open(dir_root+'\js.json').read())
This question already has answers here:
Using QIcon does not display the image
(2 answers)
Closed 1 year ago.
I created a package that contains an icon.png inside a resource-folder:
../mypackage/resource/icon.png
../mypackage/qtprogram.py
I am now trying to access the icon.png in qtprogram.py. As they are both in mypackage directory, I tried using:
QtGui.QIcon("resource//icon.png")
but it did not work. However, using the full path (in relation to the main script path) did. Since this should be a shareable package, I would like to avoid using the full path. How can I do this?
I think it should be \\ (or) r'relative-path-to-file' instead of // in QtGui.QIcon("resource//icon.png").
I would use the os library.
import os
# If you plan to use to without PyInstaller
icon_path = os.path.join(os.path.dirname(__file__), r'resources\icon.png')
# If you plan to use to pyinstaller
icon_path = os.path.join(os.path.abspath(os.getcwd()), r'resources\icon.png')
This question already has answers here:
How to delete the contents of a folder?
(26 answers)
Closed 1 year ago.
I am trying to create a python script to help clean up a folder that I create with powershell. This is a directory that contains folders named after the users for them to put stuff into.
Once they leave our site, their folder remains and for all new staff who come a folder gets created. This means that we have over 250 folders but only 100 active staff. I have a test folder that I am using so that I can get the script working and then add in extra bits like moving the directories to an old location, and then deleting them based on age. But at the moment I am working on the delete portion of the script. So far I have the below script, it runs with no errors, but it doesn't actually do anything and I am failing to see why..
It should be reading a csv file that I have of all current staff members, and then comparing that to the folders located in the filepath and then removing any folders that dont match the names in the CSV file.
The CSV file is generated from powershell using the same script that I used to create them.
import os
import pandas as pd
path = "//servername/Vault/Users$"
flist = pd.read_csv('D:/Staff List.csv')
file_name = flist['fileName'].tolist()
for fileName in os.listdir(path):
#If file is not present in list
if fileName not in file_name:
#Get full path of file and remove it
full_file_path = os.path.join(path, fileName)
os.remove(full_file_path)
Use shutil and recursively remove all old user directories and their contents.
import shutil
PATH = 'D:/user/bin/old_dir_to_remove'
shutil.rmtree(PATH)
This question already has answers here:
Implement touch using Python?
(16 answers)
Closed 4 years ago.
I have to make a script which creates a certain amount of subfolders in each main folder (dir1, dir2, and dir3). Then, inside of each subfolder, there has to be files (.txt.for example) been created constantly until I (the user) decides to stop the program. Right now, I can create subfolders (from 0 to 99) in each main folder, but I'm not sure how to create the .txt files. Any suggestions or ideas would be very much appreciated. Here's my code:
import os
folders = ["dir1", "dir2", "dir3"]
count = 0
for folder in folders:
for count in range(100):
subfolder = str(count)
newfolder = os.makedirs(os.path.join(folder, subfolder))
I'm trying to do something like this
with open("%s/01.txt" %count, "wa") as fh
so it goes to each subdirectory and create the file
You can simply open and write nothing. For example:
with open("01.txt", "w") as fh:
fh.write("")
This means that you can also write stuff in the future, if necessary. It may not be necessary to .write(), but it improves readability.