在Windows中使用Python使用绝对的Unix路径 [英] Using absolute unix paths in windows with python

查看:494
本文介绍了在Windows中使用Python使用绝对的Unix路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个将blob文件存储到硬盘驱动器的应用程序,但是此脚本必须同时在linux和Windows中运行,问题是我想给它一个从文件系统根目录开始的绝对路径,而不是相对于文件系统根目录的绝对路径.项目文件,这是因为我使用git并不想处理从同步中排除所有这些文件的情况.

I'm creating an application that stores blob files into the hard drive, but this script must run in both linux and windows, the issue is that i want to give it an absolute path from the filesystem root and not one relative to the project files, this because im using git and dont want to deal with excluding all these files from syncing.

所以我想要这样的东西:

So i would like to have something like this:

path = '/var/lib/blob_files/'
file = open(path+'myfile.blob', 'w')

并在以下位置获取unix文件:

and get a file in unix at:

/var/lib/blob_files/myfile.blob

以及Windows中的位置:

and in windows at:

C:\var\lib\blob_files\myfile.blob

它也可能相对于用户主文件夹(Unix中的/home/user和Windows中的C:/Users/User),但我想问题很相似.

it could also be relative to the user home folder (/home/user in unix and C:/Users/User in windows) but i guess the problem is very similar.

我该如何实现?是否有任何库或函数可以帮助我透明地转换此路径,而不必询问脚本一直在什么平台上运行?

How can i achieve this? is there any library or function that can help me transparently to convert this paths without having to ask in what plataform is the scrip running all the time?

在我的两个选项中,绝对是根目录,还是相对主目录中的相对目录,您建议使用哪一个?

Of my two options, absolute from root or relative from home folder, which one do you recomend to use?

在此先感谢您提供任何建议

Thanks in advance for any advice on this

推荐答案

使用os.path.abspath(),对于与用户主目录相关的文件,还使用os.path.expanduser():

Use os.path.abspath(), and also os.path.expanduser() for files relative to the user's home directory:

print os.path.abspath("/var/lib/blob_files/myfile.blob")
>>> C:\var\lib\blob_files\myfile.blob

print os.path.abspath(os.path.expanduser("~/blob_files/myfile.blob"))
>>> C:\Users\jerry\blob_files\myfile.blob

对于Windows和POSIX路径,这些将做正确的事".

These will "do the right thing" for both Windows and POSIX paths.

expanduser()中没有~,则不会更改路径,因此可以安全地在所有路径中使用它.因此,您可以轻松编写包装函数:

expanduser() won't change the path if it doesn't have a ~ in it, so you can safely use it with all paths. Thus, you can easily write a wrapper function:

import os
def fixpath(path):
    return os.path.abspath(os.path.expanduser(path))

请注意,使用的驱动器号将是Python进程当前工作目录指定的驱动器,通常是脚本所在的目录(如果从Windows资源管理器启动,并且假定您的脚本没有更改).如果要强制使其始终为C:,则可以执行以下操作:

Note that the drive letter used will be the drive specified by the current working directory of the Python process, usually the directory your script is in (if launching from Windows Explorer, and assuming your script doesn't change it). If you want to force it to always be C: you can do something like this:

import os
def fixpath(path):
    path = os.path.normpath(os.path.expanduser(path))
    if path.startswith("\\"): return "C:" + path
    return path

这篇关于在Windows中使用Python使用绝对的Unix路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆