[英]Using os.walk in Python to create a txt file for a corresponding folder and subfolders
I have been trying to create a function a that uses os.walk to go through a directory tree and create a text file that is named after the folder and only create it the text file if there is data files in a source folder and append the files to that txt file. 我一直在尝试创建一个函数,该函数使用os.walk遍历目录树并创建一个以文件夹命名的文本文件,并且仅在源文件夹中有数据文件的情况下才将其创建为文本文件并附加文件到该txt文件。 It practically makes a backup of the data files on another mapped network drive
它实际上是在另一个映射的网络驱动器上备份数据文件的
def copy_from_dest(src, dest):
os.chdir(src)
for root, dirs, files in os.walk(".",topdown=True):
for name in dirs:
newdir = (os.path.join(root, name))
os.chdir(newdir)
filenames = glob.glob('*.txt')
if len(filenames) > 1:
for fname in filenames:
add_to_file(src + newdir, fname, dest + newdir, newdir + "_data.txt")
os.chdir(src)
add_to_file
is a function that appends the files to the text file and removes the afterwards. add_to_file
是将文件附加到文本文件并随后删除文件的功能。 The problem I am facing is that the first branch of directories works perfectly the second branch (or subdirectory of subdirectory) makes the file in the second branch or second step in when it was suppose to make it in the third. 我面临的问题是,目录的第一个分支可以完美地工作,第二个分支(或子目录的子目录)使文件位于第二个分支或第二个步骤(假设要在第三个分支中)。
For example: 例如:
Root/
folder works fine Root/
文件夹工作正常 Root/folderstep1
works fine Root/folderstep1
正常运行 Root/folderstep1/folderstep2/folderstep3
incorrectly writes the txt file in folderstep2 Root/folderstep1/folderstep2/folderstep3
错误地将txt文件写入文件夹step2中 Please can anyone assist? 请任何人可以协助吗? Thanks in advance!
提前致谢!
The fourth parameter to your add_to_file
function looks suspicious: It seems that you want to construct a file name like "folderstep3_data.txt"
for the current directory, but instead you're passing something like ./folderstep1/folderstep2/folderstep3_data.txt
. 您的
add_to_file
函数的第四个参数看起来很可疑:似乎您想为当前目录构建一个类似于"folderstep3_data.txt"
的文件名,但是您要传递类似./folderstep1/folderstep2/folderstep3_data.txt
类的./folderstep1/folderstep2/folderstep3_data.txt
。 But since I don't know, how add_to_file
works internally, I cannot tell. 但是由于我不知道,
add_to_file
在内部如何工作,所以我无法确定。
Try logging the parameters to add_to_file
: 尝试将参数记录到
add_to_file
:
print (src + newdir), fname, (dest + newdir), newdir + "_data.txt"
and see, if that reveals the problem. 看看是否能解决问题。
Two additional points: 另外两点:
if len(filenames)>1:
should probably be a >0
? if len(filenames)>1:
应该应该是>0
? Or do you only want to write something, if at least two text files exist? 或者,如果至少存在两个文本文件,您是否只想写东西?
Also, since os.walk()
already walks all the subdirectories for you and also gives you a list of filenames for every one, couldn't you simply thst, if files
(the var you get from the os.walk()
-loop) contains a text file (using str.endswith('.txt')
and use only root
as directory? 另外,由于
os.walk()
已经为您遍历了所有子目录,并且还为您提供了每个文件名的列表,因此您不能简单地查看files
(从os.walk()
获得的var -loop )包含一个文本文件(使用str.endswith('.txt')
并且仅将root
用作目录?
If you have a structure like 如果你有一个像
+ src
+--+ subfolder 1
| +-- file1.txt
| +-- file2.txt
| +--+ subfolder1.2
| +-- file3.txt
+--+ subfolder 2
+-- file4.txt
then os.walk()
will give you 然后
os.walk()
会给你
root=src, dirs=['subfolder1','subfolder2'], files=[]
root=src\subfolder1, dirs=['subfolder1.2'], files=['file1.txt','file2.txt']
root=src\subfolder1\subfolder1.2, dirs=[], files=['file3.txt']
root=src\subfolder2, files=['file3.txt']
So it seems, that root
and files
should be enough information to make the call to add_file
. 因此,看来
root
和files
应该是足以调用add_file
。 So there would be no need for os.chdir()
and glob.glob()
. 因此,不需要
os.chdir()
和glob.glob()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.