OSERROR When mkdir - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: OSERROR When mkdir (/thread-37591.html) |
OSERROR When mkdir - Oshadha - Jun-29-2022
def time_now(): t = datetime.datetime.now() frmt = t.strftime('%y/%m/%d - %H:%M:%S') return frmt current_Dir = os.getcwd() log_Dir = os.path.join(current_Dir + "\\Logs\\") cSession = f'{log_Dir}{ID} Session {time_now()}.txt' os.mkdir(cSession)Whats wrong?? EDIT: I got it, Invalid File Name. Cant use special Characters. Im Dumb.. RE: OSERROR When mkdir - Axel_Erfurt - Jun-29-2022 try log_Dir = os.path.join(current_Dir, "Logs") you will create a directory with extension .txt ? RE: OSERROR When mkdir - DeaD_EyE - Jun-29-2022 Your filename is illegal. It could work on any Operating System, because of the forward slashes in the file name. A bit of history, why Windows uses Backslashes: https://www.howtogeek.com/181774/why-windows-uses-backslashes-and-everything-else-uses-forward-slashes/ You can't use a forward slash inside a Directory name or File name.If you want to sort the filenames by date, then you should use ISO8601 format, which is also convenient for lexicographic order. https://en.wikipedia.org/wiki/Filename Quote:Used as a path name component separator in Unix-like, Windows, and Amiga systems. (For as long as the SwitChar setting is set to '/ ', the DOS COMMAND.COM shell would consume it as a switch character, but DOS and Windows themselves always accept it as a separator on API level.)The normal / is as different character as ⧸ .You're using the normal forward slash, which is not allowed in file names. The ISO8601 has also colons inside. They are allowed on Windows, but if you like, you can replace them with a dash or minus.The alternative is, that you use the strftime method on the datetime object.From Wikipedia: Quote:The letter colon ꞉ (U+A789) and the ratio symbol ∶ (U+2236) are permitted in Windows filenames. In the Segoe UI font, used in Windows Explorer, the glyphs for the colon and the letter colon are identical. For easier handling of Paths you should look what pathlib is.from datetime import datetime as DateTime # renamed datetime class to DateTime # DateTime is a class from pathlib import Path def make_path(session_id): # with colons # now_iso = DateTime.now().replace(microsecond=0).isoformat() # colons replaced by _ now_iso = DateTime.now().replace(microsecond=0).isoformat().replace(":", "_") session = Path("Logs").joinpath(f"{session_id}-{now_iso}.txt") # here the directory is created and if it exists, it's also ok session.parent.mkdir(exist_ok=True) return session # make_path takes the session_id path_to_session_logfile = make_path(1337) print("Current Logfile", path_to_session_logfile) print("Parent", path_to_session_logfile.parent) print("Parent Absolute", path_to_session_logfile.parent.absolute()) As you can see, it also works on Linux. The same code should do the same on Windows, Mac or Android.The pathlib is a big help to make code more interoperable between the different Operating Systems.If you have a Path object, which points to a relative Path, you can also create from this an absolute Path. Then the current working directory is used, to construct the absolute Path, so no need to call os.getcwd() .
RE: OSERROR When mkdir - Axel_Erfurt - Jun-29-2022 For creating folder with subfolder you can use makedirs os.makedirs('folder/subfolder/')
RE: OSERROR When mkdir - DeaD_EyE - Jun-29-2022 The pathlib-Object can do this also: https://docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir Quote:Path.mkdir(mode=0o777, parents=False, exist_ok=False)¶ |