0

im trying to find out what the path of the file would be to delete it

os.remove(PATHTOFILE) 

but im not sure how to get the exact path of the file, the file would be in the same directory as the script but as this script will be on different users in the future im not sure how i would be able to detect that an change the path

Cjh1552
  • 25
  • 2
  • 1
    Do you know the absolute path of the file? I'm not quite sure what you're asking. – ltd9938 Jun 27 '18 at 17:50
  • i know that aboslute path of the file "/home/MYUSERNAME/MYFILENAME/" however wouldnt the poath be different if it was on someone else's computer if they had a different username? – Cjh1552 Jun 27 '18 at 17:53
  • 1
    Downvote because question is unclear. You are trying to find *what* file? Is each user running your script? Are they running separate copies of the script? Or do you just have one file for all users? – LTClipp Jun 27 '18 at 18:00
  • i got the answer now but what im trying to do is make a script which will compile another script(which works) but then deletes the script it used to compile, basicly i have a script which compiles another script using cython but then because cython doesnt remove the non comiled script im removing it afterwards, but as the other users are running copies of the compiling scripts the directory will not be the same – Cjh1552 Jun 27 '18 at 18:04

2 Answers2

1
import inspect, os
print inspect.getfile(inspect.currentframe()) # file name and path of script
print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # directory script is running in

See this answered question

Shakes
  • 531
  • 1
  • 5
  • 16
  • 1
    i know the post comment thing says avoid saying thanks, but thanks it works great and its just what i needed! – Cjh1552 Jun 27 '18 at 17:57
  • @Cjh1552 Glad to hear it! I'd appreciate if you could accept my answer once it allows you to (need to wait a few minutes before you can accept an answer to give everyone time to respond). – Shakes Jun 27 '18 at 18:01
  • thanks for giving me a clear answer and understanding what im trying to do, unfortunately i do not have enough rep as im new to stack exchange sorry – Cjh1552 Jun 27 '18 at 18:06
  • @Cjh1552 If you asked the question then you can mark the question as answered by clicking the check mark next to the upvote / downvote buttons, regardless of rep. Only you can say whether someone answered your question or not. – Shakes Jun 27 '18 at 18:08
  • ohh i see just did that, sorry im a noob at stack exchange sorry – Cjh1552 Jun 27 '18 at 18:10
  • @Cjh1552 Speaking from experience - we all start as noobs. It's a little tough to get started but you'll get the hang of it the more use use it. I'm still a noob myself :P Glad to have answered your question – Shakes Jun 27 '18 at 18:12
1

Use sys.argv[0]. You should read it and convert it to an absolute path with os.path.abspath(sys.argv[0]). Do this early, before any code in your Python script calls os.chdir().

Then, you can get the directory part of the script's file name.

Leo K
  • 5,189
  • 3
  • 12
  • 27