OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Replace slashes "\" in os.getcwd()

  • Thread starter Thread starter patriciajlim
  • Start date Start date
P

patriciajlim

Guest
I am trying to get the current working directory and add it to a file path with os.getcwd. Because windows uses forward slashes in the directory path, I need to change all of these slashes to back slashes for it to work in python.

What I've tried:

Code:
import os

old = getcwd()

new = old.replace("\", "/")

file_path = (new + "folder/filename")

print(file_path

The above is throwing an error of SyntaxError: EOL while scanning string literal
<p>I am trying to get the current working directory and add it to a file path with <code>os.getcwd</code>. Because windows uses forward slashes in the directory path, I need to change all of these slashes to back slashes for it to work in python.</p>
<p>What I've tried:</p>
<pre><code>import os

old = getcwd()

new = old.replace("\", "/")

file_path = (new + "folder/filename")

print(file_path
</code></pre>
<p>The above is throwing an error of <code>SyntaxError: EOL while scanning string literal</code></p>
 

Latest posts

H
Replies
0
Views
1
hohohohoho
H
Top