I exported dataframe from Python to Excel. I have no problem, but I have a question if someone can help me. Can I change the path of the file without having to go through personal folders like "crisf" in my case, to save it to the desktop? I am looking for a generic route that can be applied to all computers
xlwriter= pd.ExcelWriter(r'C:\Users\crisf\Desktop\OHH + GIT + M3.xls')
I doubt that all computers will have user called "crisf". Instead use this and get universal path (path contains user’s home directory):
import os
***some code
xlwriter= pd.ExcelWriter(os.path.expanduser("~/Desktop") + path of folder on desktop + file name)
I run print(os.path.expanduser("~\Desktop")) command on my PC and got:
C:\Users\korne\Desktop
Related
I coded simple code to import merge filter calculate dataframe, but I can use only on my computer. How can I make it usable on any computer, via relative path or a placeholder?
Code for import dataframe is down, and code working only on my machine because file path is hard-coded, I need a solution how to code this to search .xlsx on desktop for example.
katalog_CJM = pd.read_excel(
r"C:\Users\adise\Desktop\Catalogue - 20.07.-09.08.2022 - EXAMPLE\10.Katalog_Katalog_CJM_20.07.2022-09.08.2022.xlsx",
sheet_name='Katalog')
Another code for export I need to save on any desktop or documents in windows:
Venera_merge.to_excel(
r"C:\Users\adise\Desktop\Catalogue - 20.07.-09.08.2022 - EXAMPLE\EXPORT PO DOBAVLJACIMA- PROGRAM\Venera.xlsx",
index=False)
Thanks to all
From following code you will get path same as %UserProfile% and then you can concatenate further path for desktop.
import os
os.environ['USERPROFILE']
Output:
C:\\User\\user_name
first post here so sorry if it's hard to understand. Is it possible to shorten the directory in python to the location of the .py file?. For example, if I wanted to grab something from the directory "C:\Users\Person\Desktop\Code\Data\test.txt", and if the .py was located in the Code folder, could I shorten it to "\data\test.txt". I'm new to python so sorry if this is something really basic and I just didn't understand it correctly.
I forgot to add i plan to use this with multiple files, for example: "\data\test.txt" and \data\test2.txt
import os
CUR_FILE = os.path.abspath(__file__)
TARGET_FILE = "./data/test.txt"
print(os.path.join(CUR_FILE, TARGET_FILE))
With this, you can move around your Code directory anywhere and not have to worry about getting the full path to the file.
Also, you can run the script from anywhere and it will work (you don't have to move to Code's location to run the script.
You can import os and get current working directory ,this will give you the location of python file and then you can add the location of folder data and the file stored in that ,code is given below
import os
path=os.getcwd()
full_path1=path+"\data\test.txt"
full_path2=path+"\data\test2.txt"
print(full_path1)
print(full_path2)
I think this will work for your case and if it doesn't work then add a comment
I'll try to be as simple as I can be. I'm not great at these things.
On my computer, at the location "C:\Users\Oria" there's a folder called Project. That folder contains code.ipynb, and a folder called data. Inside the folder data, there's just one file called iris_features.csv
I uploaded code.ipynb to jupyter notebook, there's a line there (which is locked to changes, can't change it) which reads
irisCsvFileName = 'data' + os.sep + 'iris_fearures.csv'
df_iris_features = pd.read_csv(irisCsvFileName)
So from what I understand, it should understand that the working directory is "C:\Users\Oria\Project" and all paths will be relative to it.
However, it doesn't work. It gives the error
FileNotFoundError: [Errno 2] File data\iris_fearures.csv does not exist: 'data\\iris_fearures.csv'
When I give the full path of the iris_features.csv, it works fine. However, as I said, I can't change the given code.
What am I doing wrong? Should I upload more than just the ipynb file to jupyter notebook?
There's a typo in the code you've provided in your question:
irisCsvFileName = 'data' + os.sep + 'iris_fearures.csv'
df_iris_features = pd.read_csv(irisCsvFileName)
You've written iris_fearures.csv but later have said that the file is called iris_features. You can check your current working directory is what you expect using:
import os
cwd = os.getcwd()
And you can find more information on using file paths etc in this SO answer
you have to give the full path if you didn't open the jupyter-notebook from the folder C:\Users\Oria\Project, if you just open a .ipynb from same folder Project the paths will not be relative to that .ipynb but with the folder from where you start the jupyter
you can check the current working directory (to whom all the other paths are relatives if they are not full paths):
import os
os.getcwd()
I launched Jupyter Notebook, created a new notebook in python, imported the necessary libraries and tried to access a .xlsx file on the desktop with this code:
haber = pd.read_csv('filename.xlsx')
but error keeps popping up. Want a reliable way of accessing this file on my desktop without incurring any error response
This is an obvious path problem, because your notebook is not booted on the desktop path, you must indicate the absolute path to the desktop file, or the relative path relative to the jupyter boot directory.
You will need to enter the full path of your excel file.
First:
Open your excel file, right click on the file and click on "Copy path to clipboard".
Second:
Next paste your path in your script. Mine looks something like this:
#only using one backslash "\"
'C:\Users\...YourFileName.xlsx'
Third:
You will likely have to modify this path by adding two "\" instead of one "\" in each spot you only see one backslash.
For example, my new path would now look like this:
#using two backslashes now "\\"
'C:\\Users\\...YourFileName.xlsx'
An example of your final output will look like this:
haber = pd.read_csv('C:\\Users\\...YourFileName.xlsx')
If you are using linux
/home/(your user name)/Desktop/(your filename)
if you are on windows
C:\Users\(your user name)\Desktop\( your filename)
and if your python file is on same path where dataset file is then just give the file name with extension
Well I searched a lot and found different ways to open program in python,
For example:-
import os
os.startfile(path) # I have to give a whole path that is not possible to give a full path for every program/software in my case.
The second one that I'm currently using
import os
os.system(fileName+'.exe')
In second example problem is:-
If I want to open calculator so its .exe file name is calc.exe and this happen for any other programs too (And i dont know about all the .exe file names of every program).
And assume If I wrote every program name hard coded so, what if user installed any new program. (my program wont able to open that program?)
If there is no other way to open programs in python so Is that possible to get the list of all install program in user's computer.
and there .exe file names (like:- calculator is calc.exe you got the point).
If you want to take a look at code
Note: I want generic solution.
There's always:
from subprocess import call
call(["calc.exe"])
This should allow you to use a dict or list or set to hold your program names and call them at will. This is covered also in this answer by David Cournapeau and chobok.
You can try with os.walk :
import os
exe_list=[]
for root, dirs, files in os.walk("."):
#print (dirs)
for j in dirs:
for i in files:
if i.endswith('.exe'):
#p=os.getcwd()+'/'+j+'/'+i
p=root+'/'+j+'/'+i
#print(p)
exe_list.append(p)
for i in exe_list :
print('index : {} file :{}'.format(exe_list.index(i),i.split('/')[-1]))
ip=int(input('Enter index of file :'))
print('executing {}...'.format(exe_list[ip]))
os.system(exe_list[ip])
os.getcwd()+'/'+i prepends the path of file to the exe file starting from root.
exe_list.index(i),i.split('/')[-1] fetches just the filename.exe
exe_list stores the whole path of an exe file at each index
Can be done with winapps
First install winapps by typing:
pip install winapps
After that use the library:
# This will give you list of installed applications along with some information
import winapps
for app in winapps.list_installed():
print(app)
If you want to search for an app you can simple do:
application = 'chrome'
for app in winapps.search_installed(application):
print(app)