擅长:python、mysql、java
<p>使用<code>os.path.join()</code></p>
<pre><code>import os
from shutil import copyfile
path = "videos/"
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith((".mp4", ".mp4")):
print(os.path.join(root, name))
# copyfile(src, dst)
</code></pre>
<p>虽然认为使用绝对路径更好,但是如果需要相对路径,可以使用<a href="https://docs.python.org/2/library/os.path.html#os.path.relpath" rel="nofollow">os.path.relpath</a>。从<code>os.path.relpath</code>文档</p>
<blockquote>
<p>os.path.relpath(path[, start])</p>
<p>Return a relative filepath to path either from the current directory or from an optional start directory. This is a path computation: the filesystem is not accessed to confirm the existence or nature of path or start.</p>
<p>start defaults to os.curdir.</p>
<p>Availability: Windows, Unix.</p>
<p>New in version 2.6.</p>
</blockquote>