Get path from open file in Python

0 votes
If I have an opened file, is there an os call to get the complete path as a string?

f = open('/Users/Desktop/febROSTER2012.xls')
From f, how would I get "/Users/Desktop/febROSTER2012.xls" ?
Jan 16, 2019 in Python by ana1504.k
• 7,910 points
1,170 views

1 answer to this question.

0 votes
The key here is the name attribute of the f object representing the opened file. You get it like that:

>>> f = open('/Users/Desktop/febROSTER2012.xls')
>>> f.name
'/Users/Desktop/febROSTER2012.xls'
answered Jan 16, 2019 by SDeb
• 13,300 points

Related Questions In Python

0 votes
0 answers
0 votes
1 answer

How to get an absolute file path in Python?

Hello @kartik, Try this out: >>> import os >>> os.path.abspath("mydir/myfile.txt") 'C:/example/cwd/mydir/myfile.txt' Also ...READ MORE

answered Dec 7, 2020 in Python by Niroj
• 82,880 points
593 views
0 votes
2 answers

Extracting data from a JSON file in Python

Here is what i found and was ...READ MORE

answered Nov 27, 2018 in Python by Rupali
29,301 views
0 votes
1 answer

Create and open a file in Python

file = open('text.txt', 'w+) READ MORE

answered May 24, 2018 in Python by Nietzsche's daemon
• 4,260 points
696 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,060 views
0 votes
1 answer
0 votes
1 answer

When to use file vs open in Python?

File() has been removed since Python 3.0 ...READ MORE

answered Oct 30, 2018 in Python by SDeb
• 13,300 points
1,057 views
0 votes
1 answer

How to read numbers from file in Python?

Assuming you don't have extraneous whitespace: with open('file') ...READ MORE

answered Apr 16, 2019 in Python by SDeb
• 13,300 points
7,231 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP