0
Get current directory in python
When I am using os.getcwdb() I am getting the following warning: " DeprecationWarning: The Windows bytes API has been deprecated, use Unicode filenames instead " How to use unicode filenames method to get the current directory. (I am aware that os.getcwd() will also give the result)
9 Antworten
+ 2
import os
path=os.path.dirname(os.path.realpath(__file__))+"\\"
print(path)
+ 1
I don't work with files (or Windows, for that matter), but as far as I know, getcwdb(), which produces a bytestring representation, is better suited for Unix systems, while getcwd() works well for Windows. From the docs: "Unfortunately, some file names may not be representable as strings on Unix, so applications that need to support arbitrary file names on Unix should use bytes objects to represent path names. Vice versa, using bytes objects cannot represent all file names on Windows (in the standard mbcs encoding), hence Windows applications should use string objects to access all files."
https://docs.python.org/3/library/os.path.html
I don't know how to get the bytestring version without warnings, and I don't have a way to experiment right now.
0
Teddy Okello as I mentioned , I know that one but is there any other way using some unicode system or whatever the warning is about.
0
Teddy Okello I think os.getcwd return the path in string only.
I am concerned that I want path in binary what alternative I can use over os.getcwdb()
0
Teddy Okello actually I have read an article on internet about getting the directory path in binary using os.getcwdb()
0
Kishalaya Saha ~ swim ~ please see this.
- 1
You can use the os module.
import os
current_directory = os.getcwd()
print(current_directory)
- 1
harshit , Have you tried doing str(os.getcwd) ?
- 1
harshit what do you need to do with the cwd information? perhaps I can help you that way.