0

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')
martineau
  • 119,623
  • 25
  • 170
  • 301
Chub
  • 15
  • 5
  • seems like this is similar problem, answer here: https://stackoverflow.com/a/34275921/14071081 – kornelHub Jan 24 '21 at 19:53
  • Now I am trying with `path = os.path.relpath (r'C: \ Users \ crisf \ Desktop \ OHH + GIT + M3.xls')` . Do you know if it works for all computers? – Chub Jan 24 '21 at 20:11

1 Answers1

0

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

kornelHub
  • 114
  • 8
  • 1
    thanks you @kornelHub ! I am currently using this: `xlwriter= pd.ExcelWriter(os.path.expanduser('~/Desktop\\') + r' excel_name.xls')` – Chub Jan 24 '21 at 21:41