I'm writing a script that is supposed to check two directories, one source, and one destination, then automatically copy all the new folders from the source directory into the destination directory, then copy down the name of the folder that was copied into a text file or .csv file which will be checked in the future to avoid duplicates, because the folders in the destination directory are going to be renamed.
I'm assuming .csv files are superior to text files for storing large amounts of separate small strings, but in the csv module for python there only appears to be a way to write the iterables into the file. I'm only going to be writing in one string per row at a time. Does it make more sense to store this one string in a list than use csv.writerow, or is there a better way of doing this?
Thanks
CodePudding user response:
In python, the CSV writer will write every value in a single list as a single row. The best way to do this is by storing all of the items in a list of lists. for example
rows = [ ['Nikhil', 'COE', '2', '9.0'],
['Sanchit', 'COE', '2', '9.1'],
['Aditya', 'IT', '2', '9.3'],
['Sagar', 'SE', '1', '9.5'],
['Prateek', 'MCE', '3', '7.8'],
['Sahil', 'EP', '2', '9.1']]
Each list here will be a separate line in a csv.
Another way of doing this, if you are going the route of making everything a single string, is to use newlines to seperate each row.
EG:
rows = "value1,value2,value3\n newRow1,newRow2,newRow3"
Either way will work.
I referred to this page for the example:
https://www.geeksforgeeks.org/working-csv-files-python/
CodePudding user response:
There are different programming languages, such as Python, Java, and C# for processing large files for geospatial data. but the best way to write CSV files in Python is because you can easily extract millions of rows within a second or minute and You can read, write or You can perform many operations through Python programming.
But IF you are not a programmer then you can take the help of any application software such as EmEditor or any.
<---How to Write on application software---> CSV Editor
Open the CSV Editor. Open the editor from the Query Tools menu.
Filter & Sort the Data. From the column pop-up menu, you can set filters or sort the data.
Edit the Data. Double-click any cell to edit its content.
Save Format. Use 'File / Save As' to save the edited CSV under a given file and format.
CodePudding user response:
You can try something like this,
import pandas as pd
import os
import shutil
base_path = "C:/Users/Dell/Desktop/"
from_folders = ["New folder (2)/", "New folder/"]
to_folder = "Target/"
from_paths = list(map(lambda folder_name:base_path folder_name, from_folders))
files_list = list(map(lambda path:[path p for p in os.listdir(path)], from_paths))
target_path = base_path to_folder
[list(map(lambda path:shutil.copy(path, target_path), paths)) for paths in files_list]
pd.DataFrame(from_paths, columns = ["From Paths"]).to_csv(f"{base_path}From Paths.csv")
I think the code would work fine if you just change base_path
, from_folders
, and to_folder
according to your needs.