English 中文(简体)
How to copy a file from a network share to local disk with variables?
原标题:

If I use the following line:

shutil.copyfile(r"\mynetworksharemyfile.txt","C:TEMPmyfile.txt")

everything works fine. However, what I can t seem to figure out is how to use a variable with the network share path, because I need the r (relative?) flag. The end result I would imagine would be something like:

source_path = "\mynetworkshare"
dest_path = "C:TEMP"
file_name = "\myfile.txt"

shutil.copyfile(r source_path + file_name,dest_path + file_name)

But I have had no luck with different variations of this approach.

最佳回答

The r used in your first code example is making the string a "raw" string. In this example, that means the string will see the backslashes and not try to use them to escape \ to just .

To get your second code sample working, you d use the r on the strings, and not in the copyfile command:

source_path = r"\mynetworkshare"
dest_path = r"C:TEMP"
file_name = "\myfile.txt"

shutil.copyfile(source_path + file_name, dest_path + file_name)
问题回答

The r is for "raw string", not for relative. When you don t prefix your string with r, Python will treat the backslash "" as an escape character.

So when your string contains backslashes, you either have to put an r before it, or put two backslashes for each single one you want to appear.

>>> r"\myfile" == "\\myfile"
True

This looks like an escaping issue - as balpha says, the r makes the character a literal, rather than a control sequence. Have you tried:

source_path = r"\mynetworkshare"
dest_path = r"C:TEMP"
filename = r"my_file.txt"

shutil.copyfile(source_path + filename, dest_path + filename)

(Using an interactive python session, you can see the following:

>>> source_path = r"\mynetworkshare"
>>> dest_path = r"C:TEMP"
>>> filename = r"my_file.txt"
>>> print (source_path + filename)
\mynetworksharemy_file.txt
>>> print (dest_path + filename)
C:TEMPmy_file.txt

From your example paths, it s clear that we are discussing the Windows OS. Python implementation on this OS use a common (C) runtime library that accepts forward slashes as equivalent to back-slashes. This way you can avoid escape char issues.

source_path = "//mynetworkshare"
dest_path = "C:/TEMP"
file_name = "/myfile.txt"

Note that filename composition is handled by os.path.join:

Join one or more path components intelligently. If any component is an absolute path, all previous components (on Windows, including the previous drive letter, if there was one) are thrown away, and joining continues. The return value is the concatenation of path1, and optionally path2, etc., with exactly one directory separator (os.sep) inserted between components, unless path2 is empty. Note that on Windows, since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:foo.

import os
shutil.copyfile(os.path.join(source_path, file_name),
    os.path.join(dest_path, file_name))




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签