我的脚本执行以下操作:复制文件
./myscript pathfile.txt
- 安装包含文件系统到/ mnt /文件系统/图像文件。
在浏览路径是这样的:
/mnt/filesystem/Windows/somefile.exe
/mnt/filesystem/Program Files/somefile.exe
- 搜索pathfile.txt并从该文件中的所有路径。
提取的路径是这样的:
C:\windows\somefile.exe
C:\program Files\somefile.exe
然后我尝试的路径从pathfile.txt转换成适合我的Systempath下,所以我可以从
/mnt/filesystem/
的.exe文件复制出来。从行中删除3个第一个字符,删除C :.
- 在\上使用replace,将其设置为/。
- 添加安装目标。
现在的路径是这样的:
/mnt/filesystem/windows/somefile.exe
/mnt/filesystem/program files/somefile.exe
我试着使用路径名要复制的文件,但它不会因为路径间距的工作,大/小写等
所以我的问题:
如何动态适应路径差异,当涉及到大/小写,间距等?
我现在试图复制/mnt/filesystem/program files/somefile.exe
这确实是在/mnt/filesystem/"Program Files"/somefile.exe
,使用subprocess.call
。
我真的很陌生,所以请给我一些简单的例子。谢谢
UPDATE:
我做了一个小的脚本了我很大的剧本和制作的东西根据您的输入塞巴斯蒂安我能理解,到现在我有下面的代码。但是,代码似乎无法正常工作。该脚本正在运行,但没有文件正在被复制。
Argument = pathfile.txt
fileName = sys.argv[1]
inputFile = open(fileName, "r")
paths = inputFile.read()
src_dir = "/mnt/filesystem/"
dest_dir = "/home/user/copyhere/"
for path in re.finditer('(c(?i):\\\.*?\.exe)', paths):
hits = path.groups()
for line in hits:
line = line.replace("\\", "/").lower()
line = ntpath.splitdrive(line)[1].lstrip("\\/")
assert src_dir.endswith(os.path.sep)
for root, dirs, files in os.walk(src_dir):
for filename in files:
path = os.path.join(root, filename)
normalized_path = path[len(src_dir):].lower()
if normalized_path in paths:
shutil.copy(path, dest_dir)
paths.remove(normalized_path)
if not paths:
sys.exit()
#As mentioned the pathfile.txt is not neat and tidy, its a real mess, therefore im using regex to extract the paths.
#BELOW ARE HOW THE OUTPUT LOOKS LIKE:
#Real path:/mnt/filesystem/Program Files/ESET/ESET NOD32 Antivirus/somefile.exe
#Script path: /mnt/filesystem/program files/eset/eset nod32 antivirus/somefile.exe
#Real path: /mnt/filesystem/Program Files/Elantech/somefile.exe
#Script path: /mnt/filesystem/program files/elantech/somefile.exe
#Real path: /mnt/filesystem/Program Files (x86)/ATI Technologies/ATI.ACE/Core-Static/somefile.exe
#Script path: /mnt/filesystem/program files (x86)/ati technologies/ati.ace/core-static/somefile.exe
感谢去除不值得回答(谁,我忘了记住用户名),但这样一来这个问题保持解答,并从而得出比标记为答案但答案根本没有帮助的问题更受关注。 – Alfe
我认为你的意思是说你有一个你装载的文件系统和一个文本文件,其中列出了这个文件系统中的路径。那是对的吗?然后,如果您在Unix系统中安装该文件系统,则应该在安装点之下具有路径,因为它们在文本文件中。 _only_不同之处应该是前缀'C:'和反斜杠''''''将会斜杠'/'。空格和上/下字符应保持不变。你能证实你的情况吗? – Alfe
缩进在Python中很重要。你的问题中的代码产生'IndentationError'。 – jfs