简体   繁体   中英

How to copy a file from Windows to Windows/Linux uisng Python script

I need to copy a file from Windows to Linux and the Windows machine with the same logic. Is it Possible?

Ex:

from shutil import copyfile
copyfile(src, dst)

# src im giving as "C:\Temp\x.txt"
dst = "\\xxx.xxx.xxx.xxx\Test"

Showing error:

No such File or Directory : "\\xxx.xxx.xxx.xxx\\Test"

The directory structure of Windows and Linux are different. It might make sense to copy something to C:\\Program Files on Windows, but in Linux you're not going to store applications in /Program Files . What you can do, however, is store a file relative to one of the following locations.

The home directory

pathlib.Path.home() or os.path.expanduser("~")

The current working directory

pathlib.Path.cwd() or os.getcwd()

The current Python file

__FILE__

The temp directory

tempfile.gettempdir()


Once you have the folder you're going to start from, you can then get cross-platform subfolders like so.

In Python 3.4+, you would use the pathlib module

pathlib.Path.home() / "path" / "to" / "somewhere"

And in older versions of Python you would use the os.path module

os.path.join(os.path.expanduser("~"), "path", "to", "somewhere")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM