0
if os.path.exists('D:\Python\New folder\'+f):
    open(f+c, 'w')

The f is a character that changes in a loop. How do i add it to the rest of the 'D:\Python\New folder\' ? What i've done above makes the whole line highlighted as a comment.

1
  • Read up on raw strings and os.path.join Commented Apr 30, 2015 at 18:11

3 Answers 3

3

You cannot use a \ backslash as the last character, as \' means use an actual quote character rather then the end of the string.

You should really use os.path.join() here and have Python join the path and the filename together, and use a raw string literal for the path so that the other \ characters don't form escape sequences (\n would be a newline, for example):

path = os.path.join(r'D:\Python\New folder', f)
if os.path.exists(path):
    open(os.path.join(path, c), 'w')

os.path.join() will add the required \ path separators for you.

Sign up to request clarification or add additional context in comments.

Comments

0

Use python os.path module

os.path.join

Comments

0

Try:

if os.path.exists('D:\Python\New folder\\'+f):
    open(f+c, 'w')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.