Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 255915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:58:13+00:00 2026-05-11T21:58:13+00:00

What is the most cross platform way of removing bad path characters (e.g. \

  • 0

What is the most cross platform way of removing bad path characters (e.g. “\” or “:” on Windows) in Python?

Solution

Because there seems to be no ideal solution I decided to be relatively restrictive and did use the following code:

def remove(value, deletechars):
    for c in deletechars:
        value = value.replace(c,'')
    return value;

print remove(filename, '\/:*?"<>|')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:58:13+00:00Added an answer on May 11, 2026 at 9:58 pm

    Unfortunately, the set of acceptable characters varies by OS and by filesystem.

    • Windows:

      • Use almost any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:
        • The following reserved characters are not allowed:
          < > : ” / \ | ? *
        • Characters whose integer representations are in the range from zero through 31 are not allowed.
        • Any other character that the target file system does not allow.

      The list of accepted characters can vary depending on the OS and locale of the machine that first formatted the filesystem.

      .NET has GetInvalidFileNameChars and GetInvalidPathChars, but I don’t know how to call those from Python.

    • Mac OS: NUL is always excluded, “/” is excluded from POSIX layer, “:” excluded from Apple APIs
      • HFS+: any sequence of non-excluded characters that is representable by UTF-16 in the Unicode 2.0 spec
      • HFS: any sequence of non-excluded characters representable in MacRoman (default) or other encodings, depending on the machine that created the filesystem
      • UFS: same as HFS+
    • Linux:
      • native (UNIX-like) filesystems: any byte sequence excluding NUL and “/”
      • FAT, NTFS, other non-native filesystems: varies

    Your best bet is probably to either be overly-conservative on all platforms, or to just try creating the file name and handle errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a reliable, cross-platform way to do IPC (between two JVMs running on
We need a cross platform solution for compressing files. Our server runs on Windows
Is there a simple way to run a Python script on Windows/Linux/OS X? On
What is the shortest, most cross-platform way to make a std::unordered_set CASE-INSENSITIVE container? my_set.insert(Apples);
I am trying to figure out the most efficient way to ensure cross-browser compatibility.
Possible Duplicate: What cross platform GUI library has the most native feel for each
Please suggest me a C# cross-platform solution to copy a File with progress. The
There are a lot of flexible, complete, cross-platform, et cetera , graphical user interface
I am fairly new to Python programming, and completely new to cross-platform GUI building
When working in cross-platform project, most probably you will encounter a lot of #ifdef

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.