Linux 用于将 Windows 路径更改为 unix 路径的 Python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11253054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:08:22  来源:igfitidea点击:

Python script for changing windows path to unix path

pythonlinuxbashescaping

提问by user1489737

I want a script where I can paste a windows path as argument, and then the script converts the path to unix path and open the path using nautilus.

我想要一个脚本,我可以在其中粘贴 Windows 路径作为参数,然后脚本将路径转换为 ​​unix 路径并使用 nautilus 打开路径。

I want to be able to use the script as follows:

我希望能够使用脚本如下:

mypythonscript.py \thewindowspath\subpath\

The script currently looks like this:

该脚本目前如下所示:

import sys, os

path = "nautilus smb:"+sys.argv[1]

path = path.replace("\","/")

os.system(path)

I almost works :) The problem is that I have to add ' around the argument... like this:

我几乎可以工作:) 问题是我必须在参数周围添加 ' ......像这样:

mypythonscript.py '\thewindowspath\subpath\'

Anyone who knows how I can write a script that allows that argument is without ' , ... i.e. like this:

任何知道我如何编写允许该参数的脚本的人都没有 ' , ... 即像这样:

mypythonscript.py \thewindowspath\subpath\

EDIT: I think I have to add that the problem is that without ' the \ in the argument is treated as escape character. The solution does not necessarily have to be a python script but I want (in Linux) to be able to just paste a windows path as argument to a script.

编辑:我想我必须补充一点,问题是没有 ' 参数中的 \ 被视为转义字符。解决方案不一定必须是 python 脚本,但我希望(在 Linux 中)能够将 Windows 路径作为参数粘贴到脚本中。

回答by Jon Clements

Unless you're using a really early version of Windows: "/blah/whatever/" just works for your OP.

除非您使用的是真正早期版本的 Windows:“/blah/whatever/” 仅适用于您的 OP。

回答by Joran Beasley

may want to try

可能想尝试

my_argv_path = " ".join(sys.argv[1:])

as the only reason it would split the path into separate args is spaces in pasted path

因为它将路径拆分为单独的参数的唯一原因是粘贴路径中的空格

(eg: C:\Program Fileswould end up as two args ["c:\Program","Files"])

(例如:C:\Program Files最终会变成两个 args ["c:\Program","Files"]

回答by jfs

To avoid dealing with escapes in the shell you could work with the clipboard directly:

为了避免在 shell 中处理转义,您可以直接使用剪贴板:

import os
try:
    from Tkinter import Tk
except ImportError:
    from tkinter import Tk # py3k

# get path from clipboard
path = Tk().selection_get(selection='CLIPBOARD')

# convert path and open it
cmd = 'nautilus'
os.execlp(cmd, cmd, 'smb:' + path.replace('\', '/'))

ntpath, urlparse, os.pathmodules might help to handle the paths more robustly.

ntpath, urlparse,os.path模块可能有助于更稳健地处理路径。

回答by Amr

Actually I had something like this a while ago, I made a bash script to automatically download links I copy into clipboard, here it is edited to use your program (you first need to install xclipif you don't already have it):

实际上我不久前有过这样的事情,我制作了一个 bash 脚本来自动下载我复制到剪贴板中的链接,这里它被编辑为使用你的程序(xclip如果你还没有它,你首先需要安装它):

#!/bin/bash

old=""
new=""

old="$(xclip -out -selection c)"

while true 
do

   new="$(xclip -out -selection c)"

   if [ "$new" != "$old" ]
   then
      old="$new"

      echo Found: $new
      mypythonscript.py $new

   fi
   sleep 1
done

exit 0

Now whenever you copy something new into the clipboard, your Python script will be executed with an argument of whatever is in your clipboard.

现在,每当您将新内容复制到剪贴板时,您的 Python 脚本都将使用剪贴板中的任何内容作为参数执行。

回答by Love and peace - Joe Codeswell

#!/usr/bin/python
#! python3
#! python2
# -*- coding: utf-8 -*-
"""win2ubu.py changes WINFILEPATH Printing UBUNTU_FILEPATH
Author: Joe Dorocak aka Joe Codeswell (JoeCodeswell.com)
Usage:   win2ubu.py WINFILEPATH
Example: win2ubu.py "C:\1d\ProgressiveWebAppPjs\Polymer2.0Pjs\PolymerRedux\zetc\polymer-redux-polymer-2"
    prints /mnt/c/1d/ProgressiveWebAppPjs/Polymer2.0Pjs/PolymerRedux/zetc/polymer-redux-polymer-2
        N.B. spaceless path needs quotes in BASH on Windows   but NOT in Windows DOS prompt!
"""
import sys,os

def winPath2ubuPath(winpath):
    # d,p = os.path.splitdrive(winpath) # NG only works on windows!
    d,p = winpath.split(':')
    ubupath = '/mnt/'+d.lower()+p.replace('\','/')   
    print (ubupath)
    return ubupath

NUM_ARGS = 1
def main():
    args = sys.argv[1:]
    if len(args) != NUM_ARGS or "-h" in args or "--help" in args:
        print (__doc__)
        sys.exit(2)
    winPath2ubuPath(args[0])

if __name__ == '__main__':
    main()