user1687891
user1687891

Reputation: 864

How to pass path of the filename

I am asking a very basic question but I am confused about it.

filepath = os.path.join(dir_path, 'filename.txt')
f1 = open(r'E:/pjt/filename.txt', 'w+')

I am trying to pass the location of filename.txt to the second step. Can anybody teach me how to do it? I tried using +filepath+ but I could not.

Upvotes: 1

Views: 141

Answers (2)

benjamin
benjamin

Reputation: 77

You can use the following code snippet to get the current directory of the file, then use it for join or other commands.

import os
_cur_dir = os.path.dirname(__file__)

Upvotes: 0

Mureinik
Mureinik

Reputation: 312289

filepath is already a path to the file - just use it:

filepath = os.path.join(dir_path, 'filename.txt')
f1 = open(filepath, 'w+')
# Here ---^

Upvotes: 1

Related Questions