copy

Copy file with pathlib in Python

Copy file with pathlib in Python Question: I try to copy a file with pathlib import pathlib import shutil my_file=pathlib.Path(‘/etc/hosts’) to_file=pathlib.Path(‘/tmp/foo’) shutil.copy(my_file, to_file) I get this exception: /home/foo_egs_d/bin/python /home/foo_egs_d/src/test-pathlib-copy.py Traceback (most recent call last): File “/home/foo_egs_d/src/test-pathlib-copy.py”, line 6, in <module> shutil.copy(my_file, to_file) File “/usr/lib/python2.7/shutil.py”, line 117, in copy if os.path.isdir(dst): File “/home/foo_egs_d/lib/python2.7/genericpath.py”, line 41, in …

Total answers: 6

DataFrame modified inside a function

DataFrame modified inside a function Question: I face a problem of modification of a dataframe inside a function that I have never observed previously. Is there a method to deal with this so that the initial dataframe is not modified. def test(df): df[‘tt’] = np.nan return df dff = pd.DataFrame(data=[]) Now, when I print dff, …

Total answers: 2

Supporting the deep copy operation on a custom class?

Supporting the deep copy operation on a custom class? Question: I have a dict subclass that adds new methods and features, The main thing that I wanted to support is recursive update which works by updating every nested dict one by one unlike the dict.update method. I’m using the copy.deepcopy function just like any other …

Total answers: 2

How to create an independent copy of a function in python?

How to create an independent copy of a function in python? Question: Is it possible in python to create an un-linked copy of a function? For example, if I have a = lambda(x): x b = lambda(x): a(x)+1 I want b(x) to always return x+1, regardless if a(x) is modified not. Currently, if I do …

Total answers: 2

What is the difference between `sorted(list)` vs `list.sort()`?

What is the difference between `sorted(list)` vs `list.sort()`? Question: list.sort() sorts the list and replaces the original list, whereas sorted(list) returns a sorted copy of the list, without changing the original list. When is one preferred over the other? Which is more efficient? By how much? Can a list be reverted to the unsorted state …

Total answers: 7

Pandas: Chained assignments

Pandas: Chained assignments Question: I have been reading this link on "Returning a view versus a copy". I do not really get how the chained assignment concept in Pandas works and how the usage of .ix(), .iloc(), or .loc() affects it. I get the SettingWithCopyWarning warnings for the following lines of code, where data is …

Total answers: 1

Python copy files to a new directory and rename if file name already exists

Python copy files to a new directory and rename if file name already exists Question: I’ve already read this thread but when I implement it into my code it only works for a few iterations. I’m using python to iterate through a directory (lets call it move directory) to copy mainly pdf files (matching a …

Total answers: 5

How to deep copy a list?

How to deep copy a list? Question: After E0_copy = list(E0), I guess E0_copy is a deep copy of E0 since id(E0) is not equal to id(E0_copy). Then I modify E0_copy in the loop, but why is E0 not the same after? E0 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] for k …

Total answers: 10

Create new class instance from class method

Create new class instance from class method Question: I want to be able to create a new instance of an object by calling a method on an already instantiated object. For example, I have the object: organism = Organism() I want to be able to call organism.reproduce() and have two objects of type Organism. My …

Total answers: 6