英文:
Copying a file into multiple folders using Python
问题
我有一个文件Test.py
。我要将这个文件粘贴到两个不同的文件夹中,如下所示,但我也想根据粘贴的文件夹更改File=1
。例如,将Test.py
粘贴到文件夹1,2
中,都具有相同的File=1
。相反,我希望将Test.py
粘贴到文件夹1,2
中,分别具有File=1
和File=2
。
Test.py
的内容是
File=1
我用来粘贴的代码是
import shutil
N=[1, 2]
src_path = r"C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\Multiple copy pasting\Test.py"
for i in N:
dst_path = rf"C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\Multiple copy pasting\{i}"
File={i}
shutil.copy(src_path, dst_path)
print('Copied')
英文:
I have a file Test.py
. I am pasting this file into 2 different folders as shown below but I also want to change File=1
based on the folder it was pasted. For example, it is pasting Test.py
into folders 1,2
with the same File=1
. Instead, I want to paste Test.py
into folders 1,2
with File=1
and File=2
respectively.
The content of Test.py
is
File=1
The code I am using to paste is
import shutil
N=[1, 2]
src_path = r"C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\Multiple copy pasting\Test.py"
for i in N:
dst_path = rf"C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\Multiple copy pasting\{i}"
File={i}
shutil.copy(src_path, dst_path)
print('Copied')
答案1
得分: 1
你的指令文件={i}对所复制的文件的内容没有影响。要更改文件中的此变量,您必须直接编辑文件的文本内容。
要做到这一点,您需要使用正则表达式来检测要更改的文本。找到文本'File='后面跟着任意数量的数字的表达式是 r"File=\d*"
。
所以,不要复制,您需要做的是:
- 获取要复制的文件的内容
- 查找表达式并替换它
- 创建一个新文档,其中包含替换后的值。
这是一个示例(这里不需要使用shutil):
import re
N = [1, 2]
pattern_to_replace = re.compile(r"File=\d*")
src_path = r"C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\Multiple copy pasting\Test.py"
for i in N:
# 获取源文件的内容
with open(src_path, 'r') as src:
content = src.read()
# 修改这个内容
updated_content = re.sub(pattern_to_replace, f"File={i}", content)
# 创建新文件
dst_path = rf"C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\Multiple copy pasting\{i}"
with open(dst_path, 'w') as dst:
dst.write(updated_content)
print('已复制')
英文:
Your instruction File={i} has no effect on the content of the file you copy. To change this variable in the file, you must directly edit the text, the content, of the file.
To do so, you'll need a regex to detect the text you want to change. The expression that finds the text 'File=' followed by any number of digits is r"File=\d*"
.
So instead of copying, what you need to do is:
- get the content of the file you want to copy
- find the expression and replace it
- create a new document with the value replaced.
Here is an example (you don't need shutil here):
import re
N=[1, 2]
pattern_to_replace = re.compile(r"File=\d*")
src_path = r"C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\Multiple copy pasting\Test.py"
for i in N:
# Get content of src file
with open(src_path, 'r') as src:
content = src.read()
# Modify this content
updated_content = re.sub(pattern_to_replace, f"File={i}", content)
# Create the new file
dst_path = rf"C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\Multiple copy pasting\{i}"
with open(dst_path, 'w') as dst:
dst.write(updated_content)
print('Copied')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论