通过Windows上的子进程运行命令,文件名中包含空格

时间:2016-03-31 13:39:32

标签: python windows bash subprocess

要在python中运行命令,对于Windows,我这样做:

import subprocess
subprocess.check_output(lsCommand, shell=True)

其中lsCommand是组成bash命令的字符串列表。这有效,除非它包含一些带空格的输入。例如,复制+更改名称:

尝试cp "test 123" test123

lsCommand = ['cp', 'test 123', 'test123']
subprocess.check_output(lsCommand, shell=True)

失败,因为它认为我正在尝试cp "test" "123" test123。错误(做谷歌存储的东西):

python: can't open file 'c:\GSUtil\gsutil.py cp -n gs://folderl/test': [Errno 22] Invalid argument

然后我尝试

subprocess.check_output('cp "test 123" test123', shell=True)

同样糟糕。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

shell=True不是internal command,因此您不需要#!/usr/bin/env python from subprocess import check_call check_call(['cp', 'test 123', 'test123']) though you might need to specify a full path to cp.exe)。

在Windows上启动新子进程的内部接口使用字符串,即特定应用程序如何解释命令行。 The default MS C runtime rules (imlemented in subprocess.list2cmdline() that is called implicitly if you pass a list on Windows)在这种情况下应该可以正常工作:

shell=True

如果要使用cmd.exe,则解释命令行的程序为check_call('copy /Y /B "test 123" test123', shell=True) ,您应该使用其转义规则(e.g., ^ is a meta-character)并将命令作为字符串传递给(正如您在Windows控制台中看到的那样):

import shutil

shutil.copy('test 123', 'test123')

显然,您不需要启动外部流程,copy a file in Python

/*
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.swiperefreshmultipleviews;

import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;

/**
 * A descendant of {@link android.support.v4.widget.SwipeRefreshLayout} which supports multiple
 * child views triggering a refresh gesture. You set the views which can trigger the gesture via
 * {@link #setSwipeableChildren(int...)}, providing it the child ids.
 */
public class MultiSwipeRefreshLayout extends SwipeRefreshLayout {

    private View[] mSwipeableChildren;

    public MultiSwipeRefreshLayout(Context context) {
        super(context);
    }

    public MultiSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * Set the children which can trigger a refresh by swiping down when they are visible. These
     * views need to be a descendant of this view.
     */
    public void setSwipeableChildren(final int... ids) {
        assert ids != null;

        // Iterate through the ids and find the Views
        mSwipeableChildren = new View[ids.length];
        for (int i = 0; i < ids.length; i++) {
            mSwipeableChildren[i] = findViewById(ids[i]);
        }
    }

    // BEGIN_INCLUDE(can_child_scroll_up)
    /**
     * This method controls when the swipe-to-refresh gesture is triggered. By returning false here
     * we are signifying that the view is in a state where a refresh gesture can start.
     *
     * <p>As {@link android.support.v4.widget.SwipeRefreshLayout} only supports one direct child by
     * default, we need to manually iterate through our swipeable children to see if any are in a
     * state to trigger the gesture. If so we return false to start the gesture.
     */
    @Override
    public boolean canChildScrollUp() {
        if (mSwipeableChildren != null && mSwipeableChildren.length > 0) {
            // Iterate through the scrollable children and check if any of them can not scroll up
            for (View view : mSwipeableChildren) {
                if (view != null && view.isShown() && !canViewScrollUp(view)) {
                    // If the view is shown, and can not scroll upwards, return false and start the
                    // gesture.
                    return false;
                }
            }
        }
        return true;
    }
    // END_INCLUDE(can_child_scroll_up)

    // BEGIN_INCLUDE(can_view_scroll_up)
    /**
     * Utility method to check whether a {@link View} can scroll up from it's current position.
     * Handles platform version differences, providing backwards compatible functionality where
     * needed.
     */
    private static boolean canViewScrollUp(View view) {
        if (android.os.Build.VERSION.SDK_INT >= 14) {
            // For ICS and above we can call canScrollVertically() to determine this
            return ViewCompat.canScrollVertically(view, -1);
        } else {
            if (view instanceof AbsListView) {
                // Pre-ICS we need to manually check the first visible item and the child view's top
                // value
                final AbsListView listView = (AbsListView) view;
                return listView.getChildCount() > 0 &&
                        (listView.getFirstVisiblePosition() > 0
                                || listView.getChildAt(0).getTop() < listView.getPaddingTop());
            } else {
                // For all other view types we just check the getScrollY() value
                return view.getScrollY() > 0;
            }
        }
    }
    // END_INCLUDE(can_view_scroll_up)
}

答案 1 :(得分:0)

对于Ubuntu:

subprocess.check_output(['list', 'of', 'commands with spaces'])

for Windows:

subprocess.check_output('single command "string with spaces"')

感谢您提供我不需要shell=True的信息。