简体   繁体   中英

check to see if a directory is valid in Python

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM