move files python according value columns

  • Last Update :
  • Techknowledgy :

i suppose you haven't delimiters \ or / at the end of your values so if not you adapt the solution:

import pandas as pd
import shutil

def move(src, dest):
   shutil.move(src, dest)

df = pd.DataFrame({
   'doc_id': [1, 2, 3, 4, 5, 6, 7, 8],
   'paths': ['path1', 'path2', 'path3', 'path4', 'path5', 'path6', 'path7', 'path8'],
   'colA': ['value1', 'value2', 'value3', 'value1', 'value2', 'value3', 'value1', 'value2'],
   'colB': ['value8', 'value9', 'value10', 'value11', 'value12', 'value13', 'value14', 'value15'],
   'colC': ['othervalues1', 'othervalues2', 'othervalues3', 'othervalues4', 'othervalues5', 'othervalues6', 'othervalues7', 'othervalues8']
})
#you could use '\\'
or '/'
for delimiting folder, move calls shutil.move
df.apply(lambda row: move(row['paths'] + '\\doc' + str(row['doc_id']),
   row['colA'] + '\\' + row['colB'] + '\\' + row['colC'] + '\\' + 'doc' + str(row['doc_id'])), axis = 1)

Here is a solution that should work on every OS (assuming all destination folders are already created):

import os
import shutil
import pandas as pd

for _, row in df.iterrows():
   # Assuming filename is included in paths column
# Otherwise, the filename should be added to new_path
# and the shutil.move
new_path = os.path.join(
   row['colA'], row['colB'], row['colC'],
   os.path.basename(row['paths'])
)

shutil.move(row['paths'], new_path)

Suggestion : 2

I would like to move some files in python anycodings_pandas according to some values on python anycodings_pandas dataframe.,My script already creates the folders and anycodings_pandas subfolders necessary but I want to move the anycodings_pandas files(each row in my df is a file/document) anycodings_pandas to those folders/subfolders.,Here is a solution that should work on anycodings_shutil every OS (assuming all destination anycodings_shutil folders are already created):,As you can see I have the path for every anycodings_pandas document. I was able to create the folders anycodings_pandas but I want my script to automatically anycodings_pandas move(for example):

i suppose you haven't delimiters \ or / anycodings_shutil at the end of your values so if not you anycodings_shutil adapt the solution:

import pandas as pd
import shutil

def move(src, dest):
   shutil.move(src, dest)

df = pd.DataFrame({
   'doc_id': [1, 2, 3, 4, 5, 6, 7, 8],
   'paths': ['path1', 'path2', 'path3', 'path4', 'path5', 'path6', 'path7', 'path8'],
   'colA': ['value1', 'value2', 'value3', 'value1', 'value2', 'value3', 'value1', 'value2'],
   'colB': ['value8', 'value9', 'value10', 'value11', 'value12', 'value13', 'value14', 'value15'],
   'colC': ['othervalues1', 'othervalues2', 'othervalues3', 'othervalues4', 'othervalues5', 'othervalues6', 'othervalues7', 'othervalues8']
})
#you could use '\\'
or '/'
for delimiting folder, move calls shutil.move
df.apply(lambda row: move(row['paths'] + '\\doc' + str(row['doc_id']),
   row['colA'] + '\\' + row['colB'] + '\\' + row['colC'] + '\\' + 'doc' + str(row['doc_id'])), axis = 1)

Here is a solution that should work on anycodings_shutil every OS (assuming all destination anycodings_shutil folders are already created):

import os
import shutil
import pandas as pd

for _, row in df.iterrows():
   # Assuming filename is included in paths column
# Otherwise, the filename should be added to new_path
# and the shutil.move
new_path = os.path.join(
   row['colA'], row['colB'], row['colC'],
   os.path.basename(row['paths'])
)

shutil.move(row['paths'], new_path)

Suggestion : 3

5 days ago Dec 29, 2020  · Python provides functionality to move files or directories from one location to another location. This can be achieved using shutil.move () function from shutil module. … , 3 days ago You've got a few options when it comes to moving files around. shutil.move(), os.rename() and os.replace() are all great approaches, with all three of them using the arguments (old_path, … ,As you can see I have the path for every document. I was able to create the folders but I want my script to automatically move(for example):, 6 days ago Jan 13, 2012  · All employ the same syntax: import os import shutil os.rename ("path/to/current/file.foo", "path/to/new/destination/for/file.foo") os.replace …


import pandas as pd
import shutil def move(src, dest): shutil.move(src, dest) df = pd.DataFrame({
   'doc_id': [1, 2, 3, 4, 5, 6, 7, 8],
   'paths': ['path1', 'path2', 'path3', 'path4', 'path5', 'path6', 'path7', 'path8'],
   'colA': ['value1', 'value2', 'value3', 'value1', 'value2', 'value3', 'value1', 'value2'],
   'colB': ['value8', 'value9', 'value10', 'value11', 'value12', 'value13', 'value14', 'value15'],
   'colC': ['othervalues1', 'othervalues2', 'othervalues3', 'othervalues4', 'othervalues5', 'othervalues6', 'othervalues7', 'othervalues8']
}) #you could use '\\'
or '/'
for delimiting folder, move calls shutil.move df.apply(lambda row: move(row['paths'] + '\\doc' + str(row['doc_id']), row['colA'] + '\\' + row['colB'] + '\\' + row['colC'] + '\\' + 'doc' + str(row['doc_id'])), axis = 1)
import shutil original = r 'original path where the file is currently stored\file name.file extension'
target = r 'target path where the file will be moved\file name.file extension'
shutil.move(original, target)
import shutil original = r 'original path where the directory is currently stored\directory name'
target = r 'target path where the directory will be moved\directory name'
shutil.move(original, target)
import shutil original = r 'C:\Users\Ron\Desktop\Test_1\my_csv_file.csv'
target = r 'C:\Users\Ron\Desktop\Test_2\my_csv_file.csv'
shutil.move(original, target)
import shutil original = r 'C:\Users\Ron\Desktop\Test_1\image.jpg'
target = r 'C:\Users\Ron\Desktop\Test_2\new_image.jpg'
shutil.move(original, target)
import shutil original = r 'original path where the directory is currently stored\directory name'
target = r 'target path where the directory will be moved\directory name'
shutil.move(original, target)

Suggestion : 4

08/16/2022

Example:

{
    "name": "AzureDataLakeStorageGen2LinkedService",
    "properties": {
        "type": "AzureBlobFS",
        "typeProperties": {
            "url": "https://<accountname>.dfs.core.windows.net", 
            "accountkey": { 
                "type": "SecureString", 
                "value": "<accountkey>" 
            }
        },
        "connectVia": {
            "referenceName": "<name of Integration Runtime>",
            "type": "IntegrationRuntimeReference"
        }
    }
}

You can also store service principal key in Azure Key Vault.

{
    "name": "AzureDataLakeStorageGen2LinkedService",
    "properties": {
        "type": "AzureBlobFS",
        "typeProperties": {
            "url": "https://<accountname>.dfs.core.windows.net", 
            "servicePrincipalId": "<service principal id>",
            "servicePrincipalCredentialType": "ServicePrincipalKey",
            "servicePrincipalCredential": {
                "type": "SecureString",
                "value": "<service principal key>"
            },
            "tenant": "<tenant info, e.g. microsoft.onmicrosoft.com>" 
        },
        "connectVia": {
            "referenceName": "<name of Integration Runtime>",
            "type": "IntegrationRuntimeReference"
        }
    }
}

Example: using service principal certificate authentication

{
    "name": "AzureDataLakeStorageGen2LinkedService",
    "properties": {
        "type": "AzureBlobFS",
        "typeProperties": {
            "url": "https://<accountname>.dfs.core.windows.net", 
            "servicePrincipalId": "<service principal id>",
            "servicePrincipalCredentialType": "ServicePrincipalCert",
            "servicePrincipalCredential": { 
                "type": "AzureKeyVaultSecret", 
                "store": { 
                    "referenceName": "<AKV reference>", 
                    "type": "LinkedServiceReference" 
                }, 
                "secretName": "<certificate name in AKV>" 
            },
            "tenant": "<tenant info, e.g. microsoft.onmicrosoft.com>" 
        },
        "connectVia": {
            "referenceName": "<name of Integration Runtime>",
            "type": "IntegrationRuntimeReference"
        }
    }
}

Suggestion : 5

Using this parameter results in much faster parsing time and lower memory usage when using the c engine. The Python engine loads the data first before deciding which columns to drop.,Additional strings to recognize as NA/NaN. If dict passed, specific per-column NA values. See na values const below for a list of the values interpreted as NaN by default.,formatters default None, a dictionary (by column) of functions each of which takes a single argument and returns a formatted string,If usecols is a list of strings, it is assumed that each string corresponds to a column name provided either by the user in names or inferred from the document header row(s). Those strings define which columns will be parsed:

In[1]: import pandas as pd

In[2]: from io
import StringIO

In[3]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3"

In[4]: pd.read_csv(StringIO(data))
Out[4]:
   col1 col2 col3
0 a b 1
1 a b 2
2 c d 3

In[5]: pd.read_csv(StringIO(data), usecols = lambda x: x.upper() in ["COL1", "COL3"])
Out[5]:
   col1 col3
0 a 1
1 a 2
2 c 3
In[6]: data = "col1,col2,col3\na,b,1"

In[7]: df = pd.read_csv(StringIO(data))

In[8]: df.columns = [f "pre_{col}"
   for col in df.columns
]

In[9]: df
Out[9]:
   pre_col1 pre_col2 pre_col3
0 a b 1
In[10]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3"

In[11]: pd.read_csv(StringIO(data))
Out[11]:
   col1 col2 col3
0 a b 1
1 a b 2
2 c d 3

In[12]: pd.read_csv(StringIO(data), skiprows = lambda x: x % 2 != 0)
Out[12]:
   col1 col2 col3
0 a b 2
In[13]: import numpy as np

In[14]: data = "a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11"

In[15]: print(data)
a, b, c, d
1, 2, 3, 4
5, 6, 7, 8
9, 10, 11

In[16]: df = pd.read_csv(StringIO(data), dtype = object)

In[17]: df
Out[17]:
   a b c d
0 1 2 3 4
1 5 6 7 8
2 9 10 11 NaN

In[18]: df["a"][0]
Out[18]: '1'

In[19]: df = pd.read_csv(StringIO(data), dtype = {
   "b": object,
   "c": np.float64,
   "d": "Int64"
})

In[20]: df.dtypes
Out[20]:
   a int64
b object
c float64
d Int64
dtype: object
In [21]: data = "col_1\n1\n2\n'A'\n4.22"

In [22]: df = pd.read_csv(StringIO(data), converters={"col_1": str})

In [23]: df
Out[23]: 
  col_1
0     1
1     2
2   'A'
3  4.22

In [24]: df["col_1"].apply(type).value_counts()
Out[24]: 
<class 'str'>    4
Name: col_1, dtype: int64
In [25]: df2 = pd.read_csv(StringIO(data))

In [26]: df2["col_1"] = pd.to_numeric(df2["col_1"], errors="coerce")

In [27]: df2
Out[27]:
col_1
0 1.00
1 2.00
2 NaN
3 4.22

In [28]: df2["col_1"].apply(type).value_counts()
Out[28]:
<class 'float'> 4
   Name: col_1, dtype: int64

Suggestion : 6

shutil.move() works by first creating a copy of the file with the path defined by old_path and storing the copy in the new location, new_path. Finally, after the successful creation of the copy, Python deletes the original file located at old_path.,In cases where the original file is protected, shutil.move() will create a copy of the file in the new location, but Python will be unable to delete the original.,Instead of copying the file in question, rename() alters the path of a file, which automatically changes the file location. See below for an example of how we could apply the function:,You've got a few options when it comes to moving files around. shutil.move(), os.rename() and os.replace() are all great approaches, with all three of them using the arguments (old_path, new_path). For a more object-oriented approach, you could also use pathlib.Path().rename(), providing Path() with old_path and rename() with new_path.

import shutil

shutil.move('old_directory/test_file.txt', 'new_directory/test_file.txt')
import shutil

shutil.move(old_path, new_path)
import os
import shutil

images = [f
   for f in os.listdir() if '.jpg' in f.lower()
]

os.mkdir('downloaded_images')

for image in images:
   new_path = 'downloaded_images/' + image
shutil.move(image, new_path)
import os

os.rename('old_directory/test_file.txt', 'new_directory/test_file.txt')
import os

os.replace('old_directory/test_file.txt', 'new_directory/test_file.txt')
import pathlib

pathlib.Path('old_directory/test_file.txt').rename('new_directory/test_file.txt')