view source
print?
01
def
copyfolderos(sfolder,tfolder):
02
sourcepath
=
sfolder
03
destpath
=
tfolder
04
for
root, dirs, files
in
os.walk(sourcepath):
05
06
#figure out where we're going
07
dest
=
destpath
+
root.replace(sourcepath, '')
08
09
#if we're in a directory that doesn't exist in the destination folder
10
#then create a new folder
11
if
not
os.path.isdir(dest):
12
os.mkdir(dest)
13
print
'directory created at: '
+
dest
14
15
#loop through all files in the directory
16
for
f
in
files:
17
18
#compute current (old) & new file locations
19
oldloc
=
root
+
'\\'
+
f
20
newloc
=
dest
+
'\\'
+
f
21
22
if
not
os.path.isfile(newloc):
23
try
:
24
shutil.copy2(oldloc, newloc)
25
print
'file '
+
f
+
' copied.'
26
except
ioerror:
27
print
'file "'
+
f
+
'" already exists'
view source
print?
1
def
2
for
root, dirs, files
in
os.walk(sourcedir):
3
for
f
in
files:
4
os.unlink(os.path.join(root, f))
5
for
d
in
dirs:
6
shutil.rmtree(os.path.join(root, d))
python對檔案,資料夾的一些操作
python中對檔案 資料夾的操作需要涉及到os模組和shutil模組。建立檔案 1 os.mknod test.txt 建立空檔案 2 open test.txt w 直接開啟乙個檔案,如果檔案不存在則建立檔案 建立目錄 os.mkdir file 建立目錄 複製檔案 shutil.copyfil...
python對檔案及資料夾的一些操作
python中對檔案 資料夾的操作需要涉及到os模組和shutil模組。建立檔案 1 os.mknod test.txt 建立空檔案 2 open test.txt w 直接開啟乙個檔案,如果檔案不存在則建立檔案 建立目錄 os.mkdir file 建立目錄 複製檔案 shutil.copyfil...
python對檔案及資料夾的一些操作
python中對檔案 資料夾的操作需要涉及到os模組和shutil模組。建立檔案 1 os.mknod test.txt 建立空檔案 2 open test.txt w 直接開啟乙個檔案,如果檔案不存在則建立檔案 建立目錄 os.mkdir file 建立目錄 複製檔案 shutil.copyfil...