2

I am trying to set the folder_out to the subfolder where the source .csv is found. So I have many folders and subfolders in the main Processing folder. I want to save the the .csv file in the same folder as where it has found the file.
When I use the root, with pathlib, is that possible? And, I am getting now back IOError: [Errno 13] Permission denied: 'D:\Processing\DG_Boeblingen-..... etc. So it found the file, but can't write.

I am in Python 2.7 and using 'wb' to write.

how I set the Path and rb an wb, is using wb and rb, correct?

folder_in = Path(r'D:\Processing')
folder_out = Path(r'.')

folder_in_traj = Path(r'D:\Processing')
folder_out_traj = Path(r'.')

for incsv in folder_in.iterdir():
    outcsv = folder_out.joinpath('0new'+incsv.name)

    with open(str(incsv), 'rb') as input, open(str(outcsv), 'wb') as output:
Bjorn ten Broeke
  • 135
  • 1
  • 3
  • 10

2 Answers2

1

You are trying to save a file in root directory for which you would need sudo prviliges so if you execute the python script as super user then you should not see this issue.

Daniyal Ahmed
  • 715
  • 5
  • 11
1

I am kind of confused as to what you are trying to do here. Are you trying to output the CSV to root? In that case I think you are using Path(r'root') wrong. If you look at the documentation for pathlib, there is a class called PurePath with a method called root. You can use this to return the root.

Passing in root to Path will just return root as the path. You can try using . instead of root which might resolve to the root.

ajoseps
  • 1,871
  • 1
  • 16
  • 29
  • I see, I am trying to write a trajectoty.csv, and another panoramic.csv (both in the same source folde)r. I think that I am first need to define the panoramic and trajectory specifiek, and than start writing back to source. I am running as admin. – Bjorn ten Broeke Aug 21 '17 at 19:10
  • are you trying to open up the csv, and edit the csv in its current directory? Or are you trying to open up the csv, and write its contents to an output directory? – ajoseps Aug 21 '17 at 20:22
  • I am trying to open the file, edit it, and witte it back to the same folder where it is found in. So there are multiple folder. And i want to witte it to the folder where it is in – Bjorn ten Broeke Aug 21 '17 at 20:24
  • alright so the first part of your implementation seems to be right, but the `with open(str(incsv), 'rb') as input, open(str(outcsv), 'wb') as output:` is not quite there since you would be outputting to a different file. You just need to do `with open(str(incsv), 'rb') as input` and read the file contents, and edit it in some sort of buffer (maybe a string). You would then need to write to the same file like so: `with open(str(incsv), 'wb') as output`. Remember, you cannot read and write to the file at the same time. You need to do the write after you do the read. – ajoseps Aug 21 '17 at 20:30
  • You can also use the [fileinput](https://stackoverflow.com/questions/5453267/is-it-possible-to-modify-lines-in-a-file-in-place) module to edit files 'in place'. Note, this isn't actually in place, but is just abstracted to seem like it is. – ajoseps Aug 21 '17 at 20:32