3

Very simple question for Python 2:

I am calling specific library/function passing filename with readonly flag:

myfunction(r'/tmp/file.txt')

I wanted to replace it with variable:

filename = '/tmp/file.txt'
myfunction(r????)

How can I call that function?

vaultah
  • 44,105
  • 12
  • 114
  • 143
user2913139
  • 557
  • 2
  • 5
  • 13

3 Answers3

2

That is not readonly flag. That means raw string. You use it when you don't want escape sequences inside string to be interpreted (like \n, \t etc.) See https://docs.python.org/2.0/ref/strings.html

For your string, you don't need it since it does not contain any escape sequence. Just omit the leading r.

sureshvv
  • 4,234
  • 1
  • 26
  • 32
-1
filename = r'/tmp/file.txt'
myfunction(filename)
f43d65
  • 2,264
  • 11
  • 15
-2

in most cases you can use

myfunction(r''+filename)

if you don't want to define the r prefix in your variable. This works with b'' and u'' too.

  • 1
    This does exactly nothing. – Adam Smith May 17 '15 at 06:48
  • This is only another way if you don't want to define the r prefix in your variable. It does all it has to do. – Fame Castle May 17 '15 at 07:22
  • You don't understand the question. Please read the linked question. `r'somestring'` is not `'rsomestring'`. – Adam Smith May 17 '15 at 07:50
  • and `r'' + any_string == any_string` exactly. – Adam Smith May 17 '15 at 07:50
  • Yes. But the question was r?????. And ????? stands for the variable abd how to connec. r and ????? Si i answered the question and not if r????? is ?????? – Fame Castle May 17 '15 at 08:00
  • You don't understand the question and your answer is not only wrong but *literally* does nothing at all. The OP was asking how to turn a string into a "readonly string" which isn't a thing. He was using the syntax for a raw string literal, but that doesn't make any more sense than a readonly string. The fact that you think it does just means that you need to go read the linked question!! :-) – Adam Smith May 17 '15 at 09:00