shutil

Python shutil copyfile - missing last few lines

偶尔善良 提交于 2019-12-10 13:16:54
问题 I am routinely missing the last few kb of a file I am trying to copy using shutil copyfile. I did some research and do see someone asking about something similar here: python shutil copy function missing last few lines But I am using copyfile, which DOES seem to use a with statement... with open(src, 'rb') as fsrc: with open(dst, 'wb') as fdst: copyfileobj(fsrc, fdst) So I am perplexed that more users aren't having this issue, if indeed it is some sort of buffering issue - I would think it'd

shutil.move deleted all my photos

百般思念 提交于 2019-12-10 12:24:37
问题 for i in os.listdir(): if "jpeg" in i or "png" in i: shutil.move(os.path.join(os.getcwd(),i),"my photos") I wanted to move all photos to a folder called "my photos" which is not exist. It created a txt file called "my photos" but it is just 300 kb and when I quickview it it only shows one of my photo. So how can I get my photos back? where are they now? 回答1: I believe shutil.move simply took each file and interpreted "my photos" as the name to save each file as rather than a directory to copy

How to write a call back function for ignore in shutil.copytree

冷暖自知 提交于 2019-12-10 03:19:01
问题 I am relatively new to python. I am trying to copy a directory to another directory maintaining the structure. I am using shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False) I am trying to write a call back function for ignore. My aim is to take a list of files in a list , and copy only those files,ignoring the rest. How do we pass a list into the call back function? I wrote a simple call back function , but I get some error when I try

Is python's shutil.copyfile() atomic?

我的梦境 提交于 2019-12-10 01:52:40
问题 I'm writing a python script that copies a file using shutil.copyfile() on Linux. During the copying, other processes may be trying to read the file. Is the following sufficient to ensure that an external process doesn't get a corrupted view of the file? os.unlink(dest) shutil.copyfile(src, dest) That is, is shutil.copyfile() atomic such that other processes cannot read the destination file until after the copy operation is complete? 回答1: No, it seems to just loop, reading and writing 16KB at

Python : Copying the data from one excel file to another excel file (template) and save it as new excel file without any data loss

£可爱£侵袭症+ 提交于 2019-12-08 13:35:29
问题 I have an excel sheet (with the first row having numbers) named template.xlsx .It contains data in the sheet named ‘data’ as follows it also has a graph in the second sheet named graph like this I have a csv file named input.csv which has data in the similar format as template.xlsx but has more columns than it and the first row doesn’t have the numbers. I want to copy the data from input.csv to the first sheet of template.xlsx so that the graph according to the data in input.csv will be

error moving file with shutil

北战南征 提交于 2019-12-08 10:21:39
问题 I am trying to create a simple function that finds files that begin with a certain string and then move them to a new directory but I keep getting the following kinds of errors from shutil "IOError: [Errno 2] No such file or directory: '18-1.pdf'", even though the file exists. import os import shutil def mv_files(current_dir,start): # start of file name start = str(start) # new directory ro move files in to new_dir = current_dir + "/chap_"+ start for _file in os.listdir(current_dir): # if

shutil make_archive resulting in nested .zip files [duplicate]

纵饮孤独 提交于 2019-12-07 16:42:50
问题 This question already has an answer here : Stop shutil.make_archive adding archive to itself (1 answer) Closed last year . I've been trying to use make_archive from shutil library. Here is the code (with comprehensive comments): from shutil import make_archive make_archive( 'zipfile_name', 'zip', # archive format root_dir=None, # current working dir if None base_dir=None) # cwd if None Here is an example of directory tree: folder │ script.py └───subfolder1 │ │ file011.txt │ │ file012.txt │ │

shutil make_archive resulting in nested .zip files [duplicate]

社会主义新天地 提交于 2019-12-06 00:24:11
This question already has an answer here : Stop shutil.make_archive adding archive to itself (1 answer) Closed last year . I've been trying to use make_archive from shutil library. Here is the code (with comprehensive comments): from shutil import make_archive make_archive( 'zipfile_name', 'zip', # archive format root_dir=None, # current working dir if None base_dir=None) # cwd if None Here is an example of directory tree: folder │ script.py └───subfolder1 │ │ file011.txt │ │ file012.txt │ │ └───subfolder2 │ │ file011.txt │ │ file012.txt │ │ ... If I run my script (with code above), the result

Can anyone explain this weird behaviour of shutil.rmtree and shutil.copytree?

拥有回忆 提交于 2019-12-05 21:46:38
I'm building a relatively simple application that asks for directories, checks if they're correct, and then removes one of them and recreates it with contents of the other one. I'm encountering this weird behaviour, I'll try to explain: When I've got the destination folder window open, AND it's empty, there's an access denied exception, then I get kicked out of the folder and it gets removed. But then if it's not empty, it works just fine, no exceptions, the destination directory (from what it seems) gets emptied then filled with files from the source directory. Which is strange because it's

How to write a call back function for ignore in shutil.copytree

不羁岁月 提交于 2019-12-05 03:32:18
I am relatively new to python. I am trying to copy a directory to another directory maintaining the structure. I am using shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False) I am trying to write a call back function for ignore. My aim is to take a list of files in a list , and copy only those files,ignoring the rest. How do we pass a list into the call back function? I wrote a simple call back function , but I get some error when I try to run the copyTree function def abc(src,names): print(src) print(names) Traceback (most recent call