Reputation: 91
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.
Upvotes: 0
Views: 88
Reputation: 103
Try:
if os.path.exists('D:\Python\New folder\\'+f):
open(f+c, 'w')
Upvotes: 0
Reputation: 1121406
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.
Upvotes: 3