4

I have a program in python 2.7 that is writing certain files and directories base on user input. I need to make sure that the files and directories are valid for both linux and windows as the files will be exchanged through the two operating systems. The files will originally be created in linux and manual moved to windows.

I have checked through Python docs, stack exchange, and several pages of google without turning up any usable information which is strange because I would imagine this would be a fairly common issue.

Is there an easy solution?

Edit: I would like to validate the directory-filename incase a user enters a path that does not work for linux or windows. As an example if a user enters "Folder1/This:Error/File.txt" the program will see this as an error.

The program will run in Linux and write the files in linux but later the files will be moved to windows. The differences in forward/back-slashes is a non-issue, but other characters that may work for linux but not windows would present a problem.

Also, often times the files or directories will not be present (as they are about to be created) so I need to check that path, held in a string, would be a valid path.

Reimus Klinsman
  • 898
  • 2
  • 12
  • 23
  • 2
    What do you mean by "valid"? Do you mean you want to know if the *name* is valid? – BrenBarn Jul 22 '12 at 21:53
  • 4
    What do you mean by "valid for both linux and windows"? Line endings? – Danica Jul 22 '12 at 21:54
  • 2
    @Dougal possibly stuff like you can't have `?` or `*` in NTFS filenames, but you can in ext4 (although ill-advised), or that writing a file ending in '..' from a Linux machine to an NTFS partition has interesting results... – Jon Clements Jul 22 '12 at 22:08

3 Answers3

6

I'm not sure there is a built-in function that does what you want, so you might need to do it yourself.

Your best bet is to create a "whitelist" of characters that you know will be valid. For example: alphanumerics, underscores, etc. Then, if the filename given by the user has any characters NOT in this list, throw up an error. It doesn't matter if this whitelist leaves out a few characters that should work; getting it right 95% (as long as the 5% are false negatives) should be fine.

Keep in mind there might be some issues besides just characters that are invalid. See this question for various considerations when writing such a function.

Finally, one solution is to name the file yourself, rather than allow the user to do it. Depending on your situation, you might be able to store the file with one filename, but in the application, you see it referred to by a different name. Of course, if the user will be accessing these files directly outside of your program, this could be confusing.

Community
  • 1
  • 1
Mark Hildreth
  • 42,023
  • 11
  • 120
  • 109
  • I am in the process of making a minature modules to do what I need. I'll be loading it on github soon. – Reimus Klinsman Jul 29 '12 at 10:34
  • I've completed my module for verifying if a directory path is valid. It is currently complete with the features I need but could use some additions. If anyone is interested in using it or adding to it here is the link. https://github.com/markwingerd/checkdir – Reimus Klinsman Aug 11 '12 at 20:48
3

Your question is still a little vague. If you are talking about building/specifying directory paths when you are working with different OSes, you might want to take a look at the os and os.path modules, and in particular os.sep.

Also, when creating paths, rather than using string functions, it's better to use os.path.join()

Then there are these (perhaps this is what you mean by "valid" files and directories)?

os.path.isdir(path)

Return True if path is an existing directory. This follows symbolic links, so both islink() and isdir() can be true for the same path.

os.path.isfile(path)

Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.

There are other many os.path functions, like os.path.islink() etc, and if this is along the lines of what you are asking about, you might find your answer there.

Levon
  • 138,105
  • 33
  • 200
  • 191
-1

if you use os.path.join to create the directory string you will correctly writes the path depending on the environment you are in. then you can use os.path.isdir, as suggested above. to verify if the string points to an existing directory

fbb
  • 91
  • 5
  • os.path.join will not tell the developer if the path is too long or has invalid characters. It will only join two path's together according to the os that is being used. – Rusty Weber May 08 '15 at 23:23