Copying file from one directory to another - python

Does anyone know how I can copy/duplicate a file from one directory into another without specification of src path? I got it to work with "shutil.copy2" but it's not exactly what I am looking for since the src argument asks for the path.
My goal is to be able to copy/duplicate a file from one directory into another by filename. Has anyone done this before, if so can you guide me in the right direction? - Thanks
#----------------------------------------------------------------------------------------------------------------#
# These params will be used for specifying which template you want to copy and where to output
#----------------------------------------------------------------------------------------------------------------#
'''Load file from x directory into current working directory '''
#PullTemplate: Specify which template you want to copy, by directory path
TemplateRepo = ("/home/hadoop/BackupFolders/Case_Project/scripts")
#OutputTemplate: Let's you specify where you want to output the copied template.
#Originally set to your current working directory (u".")
OutputTemplate = (u".")
shutil.copy2(TemplateRepo, OutputTemplate)

Well if you are trying to load a file in the same project you need to have at least the folder name inside that project.
You can use json
Something like this.
import json
#someFiles is just a fold name inside the projects main folder.
with open("someFiles\\file_name", "r") as whatever_u_want:
var_of_choice = json.load(whatever_u_want)
print (var_of_choice)
once the file is open you can save the variable var_of_choice as any file name you wish where you wish using the json dump method.

Click the file you want to copy, create a duplicate of the file by choosing Duplicate key under File ( below the Jupyter logo at the top ).
Choose the file copied(file_copy), and choose Move key under File.
Choose the file path where you want to paste/move the copied file.
Rename the copied file name as you wish.
For more info, you can refer to here: https://subscription.packtpub.com/book/big_data_and_business_intelligence/9781785884870/1/ch01lvl1sec12/basic-notebook-operations

Related

Importing a file using pandas

Data visualisation: I want to import a file using pandas. I assigned a variable and file name given is correct but it shows a error message as file does not exist
Code: sample_data = pd.read_csv('sample_data.csv')
You're probably in the wrong working directory. Run os.getcwd() to check. If so, you can either change working directories with os.chdir() or give an absolute path to the file instead of a relative path.
If you're already in the right working directory, run os.listdir() to make sure the file is actually there.
Ok, in this case, what you will have to do is get the path file by right-clicking on the file and going to properties(Windows, don't know about Mac). Then just copy the file path and paste it instead of the file name. So for now, it should be something like this(as I don't know your file path):
sample_data = pd.read_csv('C:\Users\SVISHWANATH\Downloads\datasets')
Now, after the last folder, give in your file name. So now, it should look like this:
sample_data = pd.read_csv('C:\Users\SVISHWANATH\Downloads\datasets\sample_data.csv')
However, this will still not work as the slashes need to be the other way. Because of this, there will have to be an r before the quotes.
sample_data = pd.read_csv(r'C:\Users\SVISHWANATH\Downloads\datasets\sample_data.csv')
Now this should work as all the requirements are met.
You need to store the .csv file in a folder where the actual program is stored, otherwise you have to give a proper path to the file:
sample_data = pd.read_csv('filepath')

How to rename a file in certain directory? [duplicate]

This question already has answers here:
Using os.walk() to recursively traverse directories in Python
(14 answers)
Closed 2 years ago.
I hope everyone is staying safe and sound.
Could you please help me with file name change in certain directory?
I am writing a script for RDA to download documents from this website, and when I download these files from this website, file names do not have pattern, it is completely random. So I want to rename these files into certain pattern.
For example,
1st downloaded file name: filedownload.pdf
rename to: 1234567.pdf
2nd downloaded file name: extractpages.pdf
rename to: 1234568.pdf
new names are set in parameter so that part is good but as I don't know what downloaded file name will be, I can't change this file name into new name.
So what I had in mind is for each downloaded file, put it in this folder without any files, and whenever any file is located in this folder, it renames the file and put it in different folder.
Below is the code I wrote;
filepath = "originalFilePath/downloadedfile.pdf"
if os.path.isfile(filepath):
os.rename(r'originalFilePath/downloadedfile.pdf',
'newFilePath' + str(parameter['search_number']) + '.pdf')
But I would like to change file names no matter what the downloaded file name is.
Please help, thank you very much!!
I think your idea is quite correct.
First, you will list all file name in a folder before downloading anything, store that list in a variable, let's say current_files. Then you start downloading files from the web, after that, you list all the file name again and store in the second list. Now you will compare 2 lists and know what files are new (just downloaded). Then you just simply loop over these new files and rename it in a pattern that you want. I will add code in a minute
import glob
current_files = glob.glob("/path/Downloads/*.pdf")
# ['path/Downloads/abc.pdf', 'path/Downloads/xyz.pdf']
# your code to download files from the web
new_files = set(glob.glob("/path/Downloads/*.pdf")) - set(current_files)
# ['path/Downloads/just_downloaded1.pdf', 'path/Downloads/just_downloaded2.pdf']
for file in new_files:
#do the rename code here to your new files
You question is not totally clear.
Anyway I think you have an issue with your code, you have to add / after newFilePath.
Use os.renames(old, new).
os.renames(old, new)
Recursive directory or file renaming function. Works like rename(), except creation of any intermediate directories needed to make the new pathname good is attempted first. After the rename, directories corresponding to rightmost path segments of the old name will be pruned away using removedirs().
References
https://docs.python.org/3/library/os.html

How to access a file a path level below from a subfolder

Let's say I have a file called credentials.json in my current directory, an environment variable MY_CREDENTIALS=credentials.json and a script main.py that uses this environment variable via os.getenv('MY_CREDENTIALS').
Now suppose I create a subfolder and put something there like this: /subfolder/my_other_script.py.
If I print os.getenv('MY_CREDENTIALS') then I get indeed credentials.json but I can't use this file as it is in my root directory (not in /subfolder). So, how can I use this file although it is in the root directory? The only thing that works for me is to make a copy of credentials.json in /subfolder, but then I would have multiple copies of this file and I don't want that.
Thanks for your response!
Something like this could work:
from pathlib import Path
import os
FILENAME = os.getenv('MY_CREDENTIALS')
filePath = Path(FILENAME)
if filePath.exists() and filePath.is_file():
print("Success: File exists")
print(filePath.read_text())
else:
print("Error: File does not exist. Getting file from level below.")
print((filePath.absolute().parent.parent / filePath.name).read_text())
Basically, you check whether your file exists in the current folder. This will be the case, if your script is in your root folder. If it is not, you assume that you are in a subfolder. So you try to get the file from one level below (your root).
It's not totally production ready, but for the specific case you mentioned it should work. In production you should think about cases where you might have nested subfolder or your file is missing for good.
You can create symbolic link
os.symlink('<root>', '<subfolder>')
https://docs.python.org/3/library/os.html#os.symlink

Errno13, Permission denied when trying to read file

I have created a small python script. With that I am trying to read a txt file but my access is denied resolving to an no.13 error, here is my code:
import time
import os
destPath = 'C:\Users\PC\Desktop\New folder(13)'
for root, dirs, files in os.walk(destPath):
f=open(destPath, 'r')
.....
Based on the name, I'm guessing that destPath is a directory, not a file. You can do a os.walk or a os.listdir on the directory, but you can't open it for reading. You can only call open on a file.
Maybe you meant to call open on one or more of the items from files
1:
I take it you are trying to access a file to get what's inside but don't want to use a direct path and instead want a variable to denote the path. This is why you did the destPath I'm assuming.
From what I've experienced the issue is that you are skipping a simple step. What you have to do is INPUT the location then use os.CHDIR to go to that location. and finally you can use your 'open()'.
From there you can either use open('[direct path]','r') or destPath2 = 'something' then open(destPath2, 'r').
To summarize: You want to get the path then NAVIGATE to the path, then get the 'filename' (can be done sooner or not at all if using a direct path for this), then open the file.
2: You can also try adding an "r" in front of your path. r'[path]' for the raw line in case python is using the "\" for something else.
3: Try deleting the "c:/" and switching the / to \ or vice versa.
That's all I got, hope one of them helps! :-)
I got this issue when trying to create a file in the path -C:/Users/anshu/Documents/Python_files/Test_files . I discovered python couldn't really access the directory that was under the user's name.
So, I tried creating the file under the directory - C:/Users/anshu/Desktop .
I was able to create files in this directory through python without any issue.

How to Write a File to the Root Directory of a Zip File in Python

I'm trying to replace a file in a zipped archive with a script using zipfile. The file is one directory, the archive is in another. To do this, I copy everything from the original archive into another, excluding the file I want to replace. Then I write the new version of the file to replace into the new archive, close it, and delete the old archive and rename the new one. Should be easy, right? Wrong.
For whatever reason, the zipfile.write() method has this silly thing it does where it assumes that the second (optional) argument, arcname is the same as your file name, unless you specify it. So, if I have the following:
fileName = "C:\\Documents\\file"
archive.write(fileName)
I will get an archive with a subarchive called "Documents", and within that will be the file. I want the file to be in the root directory of the archive (sidenote: is 'root directory' the right term for what I'm refering to?)
Thing's I've Tried:
archive.write(fileName,'') This produced a weird file in the archive, which could not be opened.
archive.write(fileName, archive) I really thought this would work, but the system really didn't like it.
archive.write(fileNameWithoutPath) This one returned an error, since Python could no longer find the file.
So how do I specify that I want to put the file in the root directory of the archive and still specify its path so Python can find it?
Minor, and semi-related question: Is there a way to create the new archive such that it is hidden in windows explorer?
I am assuming you want a entry in the zipfile called file containin the contents of C:\Documents\file
From python docs
ZipFile.write(filename[, arcname[, compress_type]])
Write the file named filename to the archive, giving it the archive name arcname
so you want
archive.write(fileName, fileNameWithoutPath)
The first argument is the file that goes in the zip and the second is the name that is to be used in the archive, as it contains no path separators it will not create any directories.

Categories