提问人:noExplorer 提问时间:11/4/2023 最后编辑:noExplorer 更新时间:11/4/2023 访问量:28
从 Python 脚本获取路径时,通过 Cygwin 使 Windows 路径在 RSync 中工作
Making Windows paths work in RSync through Cygwin, when getting paths from Python script
问:
我正在尝试制作一个通过 Cygwin 与 RSync 交互的 Python GUI 应用程序。我已经完成了子进程,但由于路径与 Unix 不兼容,RSync 错误。
我知道路径应该被格式化为为了工作,这就是阻碍我的原因。我不知道有一种方法可以使路径以这样的格式获取,并且我尝试搜索它时无法在网上找到任何内容。/cygdrive/<drive letter>/<path>/<to>/<file or directory>/
filedialog
我试图在路径字符串的前面附加(也许不是命令应该做的),但这什么也没做,当我期望它将字符串格式化为/cygdrive/
os.path.join
/cygdrive/<drive letter>/<path>/<to>/<file or directory>
我也尝试过使用 ,并将它们直接传递给子进程命令,但什么都没有(也可能是错误的命令,但这就是我的想法)os.path.relpath
os.path.normpath
subprocess.run(["rsync", "-a" , os.path.relpath(<Source Path>)], os.path.relpath(<Destination Path>))
当前代码是这样的
import customtkinter
from tkinter import filedialog as fdiag
from tkinter import messagebox as mbox
import subprocess
import os
# Vars
global fdiagSrc, fdiagDest
fdiagDest = ''
fdiagSrc = ''
#Prepare Cygwin stuff
os.chdir(b"C:/cygwin64/bin/")
print(os.getcwd()) #Was checking if it was changing in the directory just in case
# Declare button stuff here first
def srcSel(): # Open a file dialog for Source directory selection, and assign that directory as a string to a global var
global fdiagSrc
fdiagSrc = fdiag.askdirectory()
while (len(fdiagSrc) == 0): # Show a warning in case user pressed "Cancel" or the close button on the dialog window and re-open the dialog
mbox.showwarning("Warning!", "You did not select any directory for source!")
fdiagSrc = fdiag.askdirectory()
mbox.showinfo("Src", fdiagSrc)
def destSel(): # Open a file dialog for Destination directory selection, and assign that directory as a string to a global var
global fdiagDest
fdiagDest = fdiag.askdirectory() # Show a warning in case user pressed "Cancel" or the close button on the dialog window and re-open the dialog
while (len(fdiagDest) == 0):
mbox.showwarning("Warning!", "You did not select any directory for destination!")
fdiagDest = fdiag.askdirectory()
mbox.showinfo("Dest", fdiagDest)
def GoToSendDir():
if (len(fdiagDest) == 0) or (len(fdiagSrc) == 0): # Show a warning in case they didn't select any directories
mbox.showwarning("Warning!", "You did not set any directories for Source and/or Destination! Please do that.")
else: #Proceed to transfer/mirror/clone whatever it is called
print(os.path.normpath(fdiagSrc)) # Checking the path if it is formatted properly..
subprocess.run(["rsync", " -v -a ",os.path.normpath(fdiagSrc), os.path.normpath(fdiagDest)])
[.... misc GUI stuff not related to issue ....]
答: 暂无答案
评论
os.path.splitdrive
帮助处理驱动器号部分。 可以将反斜杠转换为正斜杠。str.replace
rsync
PathLib
os.path