当文件路径中有 space 时,命令行中的 运行 exe 文件出现问题
Problem in running an exe file in command line when there is space in paths of files
我尝试通过 Windows 10 命令行通过 Python 或 VBA 代码使用 Winrar 自动归档目录中的文件夹。考虑到可能会有空格,我把所有路径都用引号括起来了。
但是我 运行 在 Python 或 VBA;
中都遇到了同样的错误
" 'C:/Program' 不是内部或外部命令,也不是可运行的程序或批处理文件。"
这是未完成的示例 Python 代码尝试;
def folders_2_Winrar(source_path:str, archive_path:str, password:str='', winrar_path:str='"C:/Program Files/WinRAR/winrar.exe"')->None:
import os
folder_list = path_entries(source_path)['folders'] ## separate function that returns a list of folders in a directory
for f in folder_list:
rar = '"'+ archive_path + os.path.basename(f) +'.rar'+'"'
f = r'"'+f+'\*.*"'
cmd = winrar_path+' a -hp' + password + ' -ep1 ' + rar + ' ' + f
os.system(cmd)
# test the function
folders_2_Winrar("E:/Test/Source", "E:/Test\Archive/","123")
什么问题我没有把握好??!!
简短回答:使用完整路径的 shorthanded 版本:"C:/Progra~1/WinRAR/winrar.exe"
更长的答案:命令提示符不喜欢路径之间的 space。通常在提示本身中,您需要使用引号来表示包含 space 的完整路径,或者使用 ~1(波浪符号)的 shorthand 约定。
这是我挖出的 web archived Microsoft knowledge base,我将其作为副本包含在此处,以防它出现故障:
Windows generates short file names from long file names in the
following manner:
Windows deletes any invalid characters and spaces
from the file name. Invalid characters include: . " / \ [ ] : ; = ,
Because short file names can contain only one period (.
), Windows
removes additional periods from the file name if valid, non-space
characters follow the final period in the file name.
For example,
Windows generates the short file name Thisis~1.txt
from the long file
name This is a really long filename.123.456.789.txt
Otherwise, Windows
ignores the final period and uses the next to the last period. For
example, Windows generates the short file name Thisis~1.789
from the
long file name This is a really long filename.123.456.789.
Windows
truncates the file name, if necessary, to six characters and appends a
tilde (~
) and a digit. For example, each unique file name created ends
with "~1
." Duplicate file names end with "~2
," "~3
," and so on.
Windows truncates the file name extension to three characters or less.
Windows translates all characters in the file name and extension to
uppercase.
Note that if a folder or file name contains a space, but
less than eight characters, Windows still creates a short file name.
This behavior may cause problems if you attempt to access such a file
or folder over a network. To work around this situation, substitute a
valid character, such as an underscore (_
), for the space. If you do
so, Windows does not create a different short file name
For example, "Afile~1.doc
" is generated from "A file.doc
" because the
long file name contains a space.
No short file name is generated from "A_file.doc
" because the file
name contains less than eight characters and does not contain a space.
The short file name "Alongf~1.txt
" is generated from the long file
name "A long filename.txt" because the long file name contains more
than eight characters.
替代答案:您可以尝试在您打算 运行:
的 cmd
中添加引号
# using f-strings for Python 3.6+
cmd = f'"{winrar_path}" a -hp {password} -ep1 {rar} {f}'
# or using good ol' format:
cmd = '"{path}" a -hp {pw} -ep1 {file} {fl}'.format(path=winrar_path, pw=password, file=rar, fl=f)
没有完整的答案。但我可以通过删除 for 循环中的引号将其设置为 运行。似乎字符串路径在有空格时必须用引号括起来。而不是当它没有时。
def folders_2_Winrar(source_path:str, archive_path:str, password:str='', winrar_path:str='"C:\Program Files\WinRAR\winrar.exe"')->None:
import os
folder_list = path_entries(source_path)['folders'] ## separate function that returns a list of folders in a directory
for f in folder_list:
rar = archive_path + os.path.basename(f) +'.rar'
f = f+'\*.*'
cmd = winrar_path+' a -hp' + password + ' -ep1 ' + rar + ' ' + f
os.system(cmd)
# test the function
folders_2_Winrar("E:/Test/Source", "E:/Test\Archive/","123")
我尝试通过 Windows 10 命令行通过 Python 或 VBA 代码使用 Winrar 自动归档目录中的文件夹。考虑到可能会有空格,我把所有路径都用引号括起来了。
但是我 运行 在 Python 或 VBA;
中都遇到了同样的错误
" 'C:/Program' 不是内部或外部命令,也不是可运行的程序或批处理文件。"
这是未完成的示例 Python 代码尝试;
def folders_2_Winrar(source_path:str, archive_path:str, password:str='', winrar_path:str='"C:/Program Files/WinRAR/winrar.exe"')->None:
import os
folder_list = path_entries(source_path)['folders'] ## separate function that returns a list of folders in a directory
for f in folder_list:
rar = '"'+ archive_path + os.path.basename(f) +'.rar'+'"'
f = r'"'+f+'\*.*"'
cmd = winrar_path+' a -hp' + password + ' -ep1 ' + rar + ' ' + f
os.system(cmd)
# test the function
folders_2_Winrar("E:/Test/Source", "E:/Test\Archive/","123")
什么问题我没有把握好??!!
简短回答:使用完整路径的 shorthanded 版本:"C:/Progra~1/WinRAR/winrar.exe"
更长的答案:命令提示符不喜欢路径之间的 space。通常在提示本身中,您需要使用引号来表示包含 space 的完整路径,或者使用 ~1(波浪符号)的 shorthand 约定。
这是我挖出的 web archived Microsoft knowledge base,我将其作为副本包含在此处,以防它出现故障:
Windows generates short file names from long file names in the following manner:
Windows deletes any invalid characters and spaces from the file name. Invalid characters include:
. " / \ [ ] : ; = ,
Because short file names can contain only one period (
.
), Windows removes additional periods from the file name if valid, non-space characters follow the final period in the file name.For example, Windows generates the short file name
Thisis~1.txt
from the long file nameThis is a really long filename.123.456.789.txt
Otherwise, Windows ignores the final period and uses the next to the last period. For example, Windows generates the short file name
Thisis~1.789
from the long file nameThis is a really long filename.123.456.789.
Windows truncates the file name, if necessary, to six characters and appends a tilde (
~
) and a digit. For example, each unique file name created ends with "~1
." Duplicate file names end with "~2
," "~3
," and so on.Windows truncates the file name extension to three characters or less.
Windows translates all characters in the file name and extension to uppercase.
Note that if a folder or file name contains a space, but less than eight characters, Windows still creates a short file name. This behavior may cause problems if you attempt to access such a file or folder over a network. To work around this situation, substitute a valid character, such as an underscore (
_
), for the space. If you do so, Windows does not create a different short file nameFor example, "
Afile~1.doc
" is generated from "A file.doc
" because the long file name contains a space.No short file name is generated from "
A_file.doc
" because the file name contains less than eight characters and does not contain a space.The short file name "
Alongf~1.txt
" is generated from the long file name "A long filename.txt" because the long file name contains more than eight characters.
替代答案:您可以尝试在您打算 运行:
的cmd
中添加引号
# using f-strings for Python 3.6+
cmd = f'"{winrar_path}" a -hp {password} -ep1 {rar} {f}'
# or using good ol' format:
cmd = '"{path}" a -hp {pw} -ep1 {file} {fl}'.format(path=winrar_path, pw=password, file=rar, fl=f)
没有完整的答案。但我可以通过删除 for 循环中的引号将其设置为 运行。似乎字符串路径在有空格时必须用引号括起来。而不是当它没有时。
def folders_2_Winrar(source_path:str, archive_path:str, password:str='', winrar_path:str='"C:\Program Files\WinRAR\winrar.exe"')->None:
import os
folder_list = path_entries(source_path)['folders'] ## separate function that returns a list of folders in a directory
for f in folder_list:
rar = archive_path + os.path.basename(f) +'.rar'
f = f+'\*.*'
cmd = winrar_path+' a -hp' + password + ' -ep1 ' + rar + ' ' + f
os.system(cmd)
# test the function
folders_2_Winrar("E:/Test/Source", "E:/Test\Archive/","123")