First of all, as @buran commented, there is no need to use str, the following suffices:
new_file_path = os.path.join(dir_path, 'mynewfile.txt')
There is a distinction between where the script exists, given by __file__, and the current working directory (usually the place from which the script was invoked), given by os.getcwd(). It is not entirely clear from the question wording which one was intended, although they are often the same. But not in the following case:
C:\Booboo>python3 test\my_script.py
But they are in the following case:
C:\Booboo>python3 my_script.py
But if you are trying to open a file in the current working directory, why should you even be bothering with making a call to os.getcwd()? Opening a file without specifying any directory should by definition place the file in the current working directory:
import os
with open('mynewfile.txt', "x") as f:
# do something with file f (it will be closed for you automatically when the block terminates
Another problem may be that you are opening the file with an invalid flag, "x" if the file already exists. Try, "w":
with open(new_file_path, "w") as f:
# do something with file f (it will be closed for you automatically when the block terminates
str()function on the result fromos.path.join(). And post full traceback you get. There is no problem with using forward slash in pathopen('mynewfile.txt', 'w')will do the job...