79823669

Date: 2025-11-18 17:45:08
Score: 0.5
Natty:
Report link

# Source - https://stackoverflow.com/a/44233443

# Posted by Dig-Doug

# Retrieved 2025-11-19, License - CC BY-SA 3.0

from os import walk

import os

# TODO - Change these to correct locations

dir_path = "/tmp/stacktest"

dest_path = "/tmp/stackdest"

for (dirpath, dirnames, filenames) in walk(dir_path):

\# Called for all files, recu\`enter code here\`rsively

for f in filenames:

    \# Get the full path to the original file in the file system

file_path = os.path.join(dirpath, f)

    \# Get the relative path, starting at the root dir

    relative_path = os.path.relpath(file_path, dir_path)

    \# Replace \\ with / to make a real file system path

    new_rel_path = relative_path.replace("\\\\", "/")

    \# Remove a starting "/" if it exists, as it messes with os.path.join

    if new_rel_path\[0\] == "/":

        new_rel_path = new_rel_path\[1:\]

    \# Prepend the dest path

    final_path = os.path.join(dest_path, new_rel_path)

    \# Make the parent directory

    parent_dir = os.path.dirname(final_path)

    mkdir_cmd = "mkdir -p '" + parent_dir + "'"

    print("Executing: ", mkdir_cmd)

    os.system(mkdir_cmd)

    \# Copy the file to the final path

    cp_cmd = "cp '" + file_path + "' '" + final_path + "'"

    print("Executing:

# Source - https://stackoverflow.com/a/44233443

# Posted by Dig-Doug

# Retrieved 2025-11-19, License - CC BY-SA 3.0

from os import walk

import os

# TODO - Change these to correct locations

dir_path = "/tmp/stacktest"

dest_path = "/tmp/stackdest"

for (dirpath, dirnames, filenames) in walk(dir_path):

\# Called for all files, recu\`enter code here\`rsively

for f in filenames:

    \# Get the full path to the original file in the file system

file_path = os.path.join(dirpath, f)

    \# Get the relative path, starting at the root dir

    relative_path = os.path.relpath(file_path, dir_path)

    \# Replace \\ with / to make a real file system path

    new_rel_path = relative_path.replace("\\\\", "/")

    \# Remove a starting "/" if it exists, as it messes with os.path.join

    if new_rel_path\[0\] == "/":

        new_rel_path = new_rel_path\[1:\]

    \# Prepend the dest path

    final_path = os.path.join(dest_path, new_rel_path)

    \# Make the parent directory

    parent_dir = os.path.dirname(final_path)

    mkdir_cmd = "mkdir -p '" + parent_dir + "'"

    print("Executing: ", mkdir_cmd)

    os.system(mkdir_cmd)

    \# Copy the file to the final path

    cp_cmd = "cp '" + file_path + "' '" + final_path + "'"

    print("Executing:

", cp_cmd)

    os.system(cp_cmd) ", cp_cmd)

    os.system(cp_cmd)
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Muzeyanto Man