Python Forum
Change escape character=no more pain ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change escape character=no more pain ?
#1
Sad 
I am sorry but i cant help it notice the pain and suffering Windows users run into using python when dealing with paths.
Python returns path to file/folder with forward slash instead of backslash as it should because backslash is an escape character.
I dont know what made the developer think using backslash as an escape character is a good idea except that they must be Linux users.

In any event, escape character needs to be changed to something more rarely used, something like |

If this was the case, problem converting forward slash to backslash would not exist. There would be no need for processing the string in order to get desired result.

How hard is it to change it ? I am sure no one is going to listen to any of my logical suggestions so i want to know if i can change it my self and just create a new branch of python calling it a wython for windows users or something.

Thanks !
Reply
#2
The backslash was an escape character long before the first Windows system was born. Why did the writer of DOS choose it as a OS path separator?

You can submit a patch to the core python developers, but I must warn you that there is about 0% chance that your suggestion is adopted.

See also Who is python-forum.io ?
metulburr likes this post
Reply
#3
Your post clearly shows that you have little or no experience and has given no consideration whatsoever about backward compatibility issues and other problems (e.g. | has meaning on Unux-like systems) associated with your suggestion. Python provides enough tools to painlessly deal with paths on Windows. The effort to learn and use them is negligible.
And please, don't speak on behalf of the whole windows community. You simply project your experience to the whole community.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
My experience is that lot of pain and suffering is coming from lack of knowledge. It can be illustrated by example of people carrying heavy items long distance and lacking the knowledge that wheel is already invented.

It is believed that Socrates has said: “The Beginning of Wisdom is the Definition of Terms.” So what is 'pain and suffering' and what causes it? Some code snippets can lead this thread from opinion based discussion to more fact based one.

I don't have enough information but my guess is that raw strings, os.path, pathlib can remedy some or all the pain and suffering.

As for 'simple' solution of | - in many cases the cure is worse than the disease and I believe that this is one of these cases.

This is opinionated post carrying no practical value. If some specific problems will be raised I believe that members of this forum will try their best to help.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
You should better ask how to handle Windows specific paths instead of changing a whole language and breaking all existing Code on the world.

My rules for Windows-Paths:
  1. If your path contains a backslash, put an r (for regex raw string) in front of the quotes: windows_dir = r"C:\Windows"
    This will prevent interpreting escape sequences. A backslash at the end will escape the quote.
  2. If your path ends with a backslash, then remove it. Otherwise, it will escape your quote and this is a SyntaxError.
  3. If your path is constructed/concatenated by another paths, then use pathlib.Path
    from pathlib import Path
    
    
    windows_path = Path(r"C:\Windows")
    system32 = windows_path / "system32"
    print(system32)

Just use as much as you can the pathlib module to handle Paths.
If you doing it right, this will work on Linux, Mac and Windows with the same code.

For well-known Paths on the different operating systems, you could use the third party package appdirs.
import appdirs


print("User-Config:".ljust(30), appdirs.user_config_dir())
print("User-Cache:".ljust(30), appdirs.user_cache_dir())
print("User-Data".ljust(30), appdirs.user_data_dir())
print("User-Logs:".ljust(30), appdirs.user_log_dir())
print("User-State-Dir".ljust(30), appdirs.user_state_dir())
Output:
User-Config: /home/deadeye/.config User-Cache: /home/deadeye/.cache User-Data /home/deadeye/.local/share User-Logs: /home/deadeye/.cache/log User-State-Dir /home/deadeye/.local/state
If my app should create a directory in the user config directory:
from pathlib import Path

import appdirs


app_config = Path(appdirs.user_config_dir(), "appname", "config.ini")

# creating the directory and do nothing if it already exists
# app_config.parent.mkdir(exist_ok=True)
# opening the file to write
# with app_config.open("wt", encoding="utf8") as fd:
#     fd.write("Hello Config")
In my case, app_config is: '/home/deadeye/.config/appname/config.ini'
You would get a different result.

But if you read the documentation about appdirs, you've more options.
I like wrapping all Paths into Path objects to handle them better.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
os.path.join("C:", "Windows", "system32") should also work. (I don't have a Windows system to try it)
Reply
#7
(Oct-21-2020, 03:33 PM)Gribouillis Wrote: os.path.join("C:", "Windows", "system32") should also work. (I don't have a Windows system to try it)

Oldstyle: os.path.join("C:", "Windows", "system32")
Existing Path object: existing_path.join("system32")
New instance of Path object: Path("C:", "Windows", "system32")
Use of / operator on Path object: Path("C:") / "Windows" / "system32"

A Path object is immutable and the / operator joins a Path with Path or Path with str and return a new Path object.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
(Oct-21-2020, 03:22 PM)DeaD_EyE Wrote: r (for regex)

r stands for raw string
DeaD_EyE likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Quote:I am sorry but i cant help it notice the pain and suffering Windows users run into using python when dealing with paths.
Python is not the only language that uses that character as the escape character. Changing the escape character now would be changing 50 years of common knowledge.

You can make a branch to modify it to your liking. But even windows users are use to that being the escape character.
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020