Overwriting a file that already exists in the directory during shutil.move operation - move

I have a Source File which I'm moving to an Archive directory using
shutil.move(srcfile, dstdir)
But when the same file already exists in the archive destination directory it throws an error saying can't move the file exists already. So I would like to overwrite the existing file. Is there a way to do that?

I had this same question. In case anyone else is looking for a solution, here's what I did.
According to the shutil documentation, there isn't a direct way to do this. However, there is an easy fix using os.remove(). Assuming that you are in the source directory and you are moving the file 'srcfile' to 'dstdir':
import shutil, os
try:
os.remove(dstdir+'srcfile')
except OSError:
pass
else:
shutil.move(srcfile, dstdir)`
This tries to clear 'dstdir' of 'srcfile' before it moves the file.

Related

Golang beginner, on Windows: System cannot find file specified

Only just now starting with golang, with only a small amount of programming experience before this. I'm trying to create a script that will summarize certain things from a csv file, but I haven't even gotten past testing out file reading yet.
I was having trouble reading the excel files, and kept getting the "System cannot find file specified" error. So I thought I'd see if I could at least get it to read a simple text file, using an example from golangbot, which looks like this:
package main
import (
"fmt"
"io/ioutil"
)
func main() {
data, err := ioutil.ReadFile("test.txt")
if err != nil {
fmt.Println("File reading error", err)
return
}
fmt.Println("Contents of file:", string(data))
}
That simple. The text file is located within the same folder (in %USERPROFILE%/go/src, and /go/ is my GOPATH) as the actual code file I'm attempting to run, and yes, it is called "test.txt". Yet, every attempt to run gives me the same error message, that the system cannot find the specified file (test.txt).
Running any other kind of .go file or building one from this location works just fine. I'm seen others with this error, but it seemed like it was always to do with the GOPATH being set wrong.
I'm frustrated that I even have to ask about something like this, but it's all I could think of right now. Is there something wrong with the locations of my files or the GOPATH itself, or is this something different?
Thank you
Welp, the problem was solved. Turns out, the actual name of the txt file was test.txt.txt. Thanks to notepad and my own lack of awareness.
Bit embarrassing, really. Changing the name worked.
when you are trying this with your notepad. Be cautious while saving it.
The file type should be compatible
File -> save as >
FileName : FileName.go
Save as type : All Files
Now execute as ("folder path">go run FileName.go
Then the result. Kudos
I have been having a similar issue as well. I would try to do this in order to fix it. delete the file and create a new one. That is a simple solution in my opinion. Make sure to copy the code and then paste onto the new file. Mkdir
cd into that directory touch or nano
create a new file. then open that file. then do go run that file name.
it should work.

Biopython: SeqIO.parse() FileNotFoundError

I'm new in Bioinformatics and Biopython, so I have some difficulties with it.
I was reading the Biopython (SeqIO) documentation, but when I try to execute some SeqIO.parse() commands I get FileNotFoundError.
For example, I want to get "example.fasta" file (which I don't have it on my PC). I try to do it with this command:
for record in SeqIO.parse("example.fasta", "fasta"):
print(record.id)
But, all I get is FileNotFoundError: [Errno 2] No such file or directory
Can someone help me with this?
My understanding is that FileNotFoundError occurs when the code tries to open a file on your computer and does not find it.
This can happen either because you simply do not have this file, or you gave the name with a typo, or the path to the file is not correct (This is an important notion: the path to the file should be absolute, or relative to the current working directory (usually the one from which you executed the python script)).
As suggested in the comments to your question, you seem to be expecting SeqIO.parse to get the file for you. This is not the case. The first argument you give to this function (in the example "example.fasta") is the path to an existing file that you want to "parse", that is, interpret its information content and make this content available to the rest of your program in a convenient form.
So in order to get this example working, you first need to get a fasta file. If you do not already have one, you can download some manually from genbank, or find one in the biopython installation (if you installed it from source and know where the source code is located), for instance in Tests/Quality/example.fasta.

Windows 7 Folder broken creation glitch

I wrote a program in python that created folders. I'm having difficulty removing some of the folders due to this glitch.
I already figured out how to prevent it. But for the folders already created are irremovable. I get this error message when trying to delete them.
Could not find this item. This is no longer located at ... Verify the items location and try again [Try Again][Cancel]
I tried removing the folders by typing dir /x into a console
and then using the shorthand of the folder name with the del program. ex: del FOLDER~1
But that only works some of the time.
The only difference between the broken and non broken folders is the space at the end of the folder name when creating.
How to make a broken directory:
mkdir "broken folder /"
How to make a normal directory
mkdir "normal folder/"
Extra info:
The folders can still be used. The files inside can be deleted. Just not the folder itself or its parent folder.
When this glitch occurs in python using os.mkdir it also creates two directories with the exact same name. Only one can be deleted regularly.
I wrote a python script that fixes all the broken folders. So if anyone hits this issue. Hope this helps. Just drop into whatever folder is full of broken folders. Its kind of poorly made. But gives an idea about what you need to do.
from glob2 import glob
import os
import shutil
#find all folders
folders = glob("./**/")
# for each folder check if they exist and rename them to have an A at the end of their name.
for fold in folders:
if fold != ".\\":
if os.path.exists(fold):
name = fold.rsplit("\\", 2)[-2] + "A"
print(name)
print(fold)
os.rename(fold, name)

Copy Files to Directory and Overwrite if theres any Collisions

I am attempting to move files from directory A to directory B. When I move the files into directory B, if there are any collisions then I want the files from A to overwrite the collisions in B.
Will my following code do this?
SetOverwrite ifnewer
CopyFiles \SILENT "Directories/Directory_A/" "Directories/Directory_B"
Note: I am aware of the function MoveFileFolder but I am experiencing buggy behaviour where the files are not being moved when the function is called.
according to the docs, SetOverwrite only works with the File command. if your overwriting criteria is based on the file date, you might want to have a look at GetTime, if it's file existence use IfFileExists

File Rename Function on Microware OS-9

Is there a system call (C API function, not assembler) to rename a file in Microware OS-9 3.03?
If not, is there an accepted way to accomplish this task?
To answer the second part: I worked around this by doing an open on the file. If the open succeeded then I infer the file exists. If the open failed then I check the error in errno. If it's EOS_PNNF, then I infer the file does not exist. Anything else is an error.
I would still like to know about the first part of the question.

Categories

Resources