Get current directory in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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)

9th Jun 2019, 9:02 PM
harshit
harshit - avatar
9 Answers
+ 2
import os path=os.path.dirname(os.path.realpath(__file__))+"\\" print(path)
2nd Aug 2021, 5:02 AM
Sancho Godinho
Sancho Godinho - avatar
+ 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.
11th Jun 2019, 1:53 AM
Kishalaya Saha
Kishalaya Saha - avatar
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.
10th Jun 2019, 8:41 AM
harshit
harshit - avatar
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()
10th Jun 2019, 9:57 AM
harshit
harshit - avatar
0
Teddy Okello actually I have read an article on internet about getting the directory path in binary using os.getcwdb()
10th Jun 2019, 4:02 PM
harshit
harshit - avatar
0
Kishalaya Saha ~ swim ~ please see this.
10th Jun 2019, 7:10 PM
harshit
harshit - avatar
- 1
You can use the os module. import os current_directory = os.getcwd() print(current_directory)
9th Jun 2019, 9:35 PM
Teddy Okello
Teddy Okello - avatar
- 1
harshit , Have you tried doing str(os.getcwd) ?
10th Jun 2019, 9:39 AM
Teddy Okello
Teddy Okello - avatar
- 1
harshit what do you need to do with the cwd information? perhaps I can help you that way.
10th Jun 2019, 10:07 AM
Teddy Okello
Teddy Okello - avatar