0

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
bouteillebleu
2,51323 silver badges32 bronze badges
asked Sep 24, 2020 at 17:01
2
  • 1
    R is a variable here, not a string. You have no R variable Commented Sep 24, 2020 at 17:03
  • Is R a variable? If not, then make it a string 'R' Commented Sep 24, 2020 at 17:03

3 Answers 3

1

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.

answered Sep 24, 2020 at 17:03
Sign up to request clarification or add additional context in comments.

Comments

0

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.

answered Sep 24, 2020 at 17:03

Comments

0

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).

answered Sep 24, 2020 at 17:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.