OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

OS walk result not as expected

  • Thread starter Thread starter Mikef
  • Start date Start date
M

Mikef

Guest
Using os.walk in Python 3.10
My code

Code:
import os

def generate_dir_report(path, report_file_path):
    print("\n ---- Starting generate_dir_report ----")
 
    # open file
    f = open(report_file_path,  "w")

    for root, dirs, files in sorted(os.walk(path)):
        for file_name in sorted(files):
            # write the `root` string into the `out` file as a single line>
            # fix print(root)
            print(root)
            f.write(root + "\n") 

            # write the `root` string into the `out` file as a single line>
            for file_name in sorted(files):
                # write the `file_name` string into the `out` file as a single line
                print(file_name)
                f.write(file_name + "\n")

    f.close()
    print(" ---- Done ----\n")

generate_dir_report('data/dir-top','dir-report.txt')

Expected results

Code:
data/dir-top
file1.txt
file2.txt
data/dir-top/dir1
file3.txt
data/dir-top/dir2
file6.txt
file7.log
data/dir-top/dir3
file4.log
data/dir-top/dir3/dir4
file5.txt

Actual result

Code:
---- Starting generate_dir_report ----
data/dir-top
file1.txt
file2.txt
data/dir-top
file1.txt
file2.txt
data/dir-top/dir1
file3.txt
data/dir-top/dir2
file6.txt
file7.log
data/dir-top/dir2
file6.txt
file7.log
data/dir-top/dir3
file4.log
data/dir-top/dir3/dir4
file5.txt
 ---- Done ----

The first expected entry (first 3 lines) seem to be duplicated 2x in my actual results. The same happens again so that the total expected lines are 12 and the total actual lines are 18.

I have duplicated the issue on Windows, and a web-based virtualization I am using; the results are consistent in both cases.
<p>Using os.walk in Python 3.10<br />
My code</p>
<pre><code>import os

def generate_dir_report(path, report_file_path):
print("\n ---- Starting generate_dir_report ----")

# open file
f = open(report_file_path, "w")

for root, dirs, files in sorted(os.walk(path)):
for file_name in sorted(files):
# write the `root` string into the `out` file as a single line>
# fix print(root)
print(root)
f.write(root + "\n")

# write the `root` string into the `out` file as a single line>
for file_name in sorted(files):
# write the `file_name` string into the `out` file as a single line
print(file_name)
f.write(file_name + "\n")

f.close()
print(" ---- Done ----\n")

generate_dir_report('data/dir-top','dir-report.txt')
</code></pre>
<p>Expected results</p>
<pre><code>data/dir-top
file1.txt
file2.txt
data/dir-top/dir1
file3.txt
data/dir-top/dir2
file6.txt
file7.log
data/dir-top/dir3
file4.log
data/dir-top/dir3/dir4
file5.txt
</code></pre>
<p>Actual result</p>
<pre><code>---- Starting generate_dir_report ----
data/dir-top
file1.txt
file2.txt
data/dir-top
file1.txt
file2.txt
data/dir-top/dir1
file3.txt
data/dir-top/dir2
file6.txt
file7.log
data/dir-top/dir2
file6.txt
file7.log
data/dir-top/dir3
file4.log
data/dir-top/dir3/dir4
file5.txt
---- Done ----
</code></pre>
<p>The first expected entry (first 3 lines) seem to be duplicated 2x in my actual results. The same happens again so that the total expected lines are 12 and the total actual lines are 18.</p>
<p>I have duplicated the issue on Windows, and a web-based virtualization I am using; the results are consistent in both cases.</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top