user13323547
user13323547

Reputation:

Unable to define a filepath

I am trying to open a file in python. It is in a folder named R and the file I want to open is called PROTO.rtf

This is the code I have so far:

filepath = os.path.join(R, "PROTO.rtf")
file = open(filepath)
content = f.read()

This is the error it throws when I try to create my filepath:

NameError: name 'R' is not defined

Upvotes: 0

Views: 96

Answers (3)

LMKR
LMKR

Reputation: 647

Define Variable R before using it. Once you defined your path you can always verify you file using os.path.isfile(file_name_with_path).

Upvotes: 0

noah
noah

Reputation: 2776

You currently have R being used as a variable. You didn't define a variable R. You want a string. If R is a folder in the current directory you want filepath = os.path.join("R", "PROTO.rtf"). If it is not in the current directory you will want filepath = os.path.join("/full/path/to/R", "PROTO.rtf")

This page includes a few good examples.

Upvotes: 0

Jackson
Jackson

Reputation: 78

R would need to be a string in this case. From your code snippet, it is not defined. You'd need to include quotes around it to make it a string literal, or define it above.

Upvotes: 1

Related Questions