famousdavis
Posts: 16
Joined: Tue Mar 24, 2015 8:55 pm

How to write or delete files on a Windows shared folder

Tue May 19, 2015 11:29 pm

Hi, I'm using Python to store surveillance photos outside my home onto a Windows 2008 shared folder. So, I've successfully mounted the shared folder in my Pi, and it automounts after rebooting, too. I can read anything in the shared folder (command line, File Manager, whatever). But the only way my Python script will work is if I run the Python script from the command line using sudo. Using just my Pi's userid (which isn't "pi" but an account I created for myself) will lead to a "permission denied" message.

Googling turned up several ideas involving file permissions in /etc/fstab, but they didn't seem to work; I still got "permission denied" when trying to write or delete files in the shared folder. I'm looking for an easy, elegant solution.

If I run the Python script from command line using sudo, everything works just dandy.

Thoughts?

User avatar
DougieLawson
Posts: 35557
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: How to write or delete files on a Windows shared folder

Tue May 19, 2015 11:32 pm

What options did you pass on the mount command? (Or set in /etc/fstab?)
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

famousdavis
Posts: 16
Joined: Tue Mar 24, 2015 8:55 pm

Re: How to write or delete files on a Windows shared folder

Tue May 19, 2015 11:35 pm

Here's the relevant line. I did try doing something differently setting file permissions according to something I found on Google, but it wasn't successful:

Code: Select all

//192.168.0.100/RaspberryPi /mnt/raspberrypi/ cifs username=Pi,password=raspberry 0 0

lazarus78
Posts: 236
Joined: Thu Jul 25, 2013 5:16 pm

Re: How to write or delete files on a Windows shared folder

Tue May 19, 2015 11:42 pm

shouldnt the username and password be that of the windows computer, since that is what you are connecting to?

famousdavis
Posts: 16
Joined: Tue Mar 24, 2015 8:55 pm

Re: How to write or delete files on a Windows shared folder

Tue May 19, 2015 11:49 pm

Yes it should be - and it is. :) I setup an account on the Windows server called Pi to match what comes with the Raspberry Pi.

User avatar
DougieLawson
Posts: 35557
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: How to write or delete files on a Windows shared folder

Tue May 19, 2015 11:52 pm

Try adding

Code: Select all

uid=pi
so that it's mounted with ownership for pi.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

famousdavis
Posts: 16
Joined: Tue Mar 24, 2015 8:55 pm

Re: How to write or delete files on a Windows shared folder

Wed May 20, 2015 12:52 am

Thanks for the suggestion - that got me further down the road, but I haven't yet arrived.

By adding that to /etc/fstab, now I can use File Manager and read/write/delete/create files and folders on the Windows share. That's going to be more convenient for me when I want to copy Pi files to my Windows server (for easy backups, for instance). So, thank you!

That didn't resolve the Python issue where I'd like to execute a Python script without sudo. When I try without sudo, I get an IO error:

Code: Select all

Unexpected error: <class 'IOError'>
Traceback (most recent call last):
  File "/home/william/Documents/Python_Projects/Scripts/Camera_Monitor_with_Spyder.py", line 67, in <module>
    sys.exit(1)
SystemExit: 1
But I can still run the Python script using sudo and it writes to the Windows share like it did before, so I've lost nothing but I did gain easier control over the Windows share using File Manager.

Other ideas?

ktb
Posts: 1380
Joined: Fri Dec 26, 2014 7:53 pm

Re: How to write or delete files on a Windows shared folder

Wed May 20, 2015 4:55 am

Are you willing to post the content of your Python script? It would probably help us to determine what access/permissions/group-membership is required to run without sudo. That error seems pretty generic to me.

famousdavis
Posts: 16
Joined: Tue Mar 24, 2015 8:55 pm

Re: How to write or delete files on a Windows shared folder

Wed May 20, 2015 8:07 pm

Sure - but you'll quickly see I'm just learning Python. :)

Here's the script. On the Pi, the account I'm running under is William. The mount to the Windows share uses a Windows account called Pi, the credentials for which are included in the /etc/fstab.

Code: Select all

# Imports
import time
from datetime import datetime as dt
from datetime import timedelta as td
import sys
import os
import picamera
import shutil

# Variables
camera = picamera.PiCamera()
camera.vflip = True
camera.hflip = True
camera.awb_modes = 'auto'
#camera.awb_modes = 'off'
#camera.awb_gains = (1.0, 1.1)
camera.exposure_modes = 'auto'
c_brightness = 65
c_contrast = 15

v_now = dt.now().strftime("%H")
v_ymd = dt.now().replace(minute=00, second=00) + td(hours=1)

print (v_ymd)

def copyFile(src, dest):
    try:
        shutil.copy2(src, dest)
    # eg. src and dest are the same file
    except shutil.Error as e:
        print('Error: %s' % e)
    # eg. source or destination doesn't exist
    except IOError as e:
        print('Error: %s' % e.strerror)

for nu_loop in range(0,10000,1):
    photo_count = 0
    try:
        while dt.now() < v_ymd:
            if not os.path.exists("/home/william/Documents/Python_Projects/hour" + v_now):
                os.mkdir("/home/william/Documents/Python_Projects/hour" + v_now)
            if not os.path.exists("/mnt/raspberrypi/pitest/hour" + v_now):
                os.mkdir("/mnt/raspberrypi/pitest/hour" + v_now)
            if (0 <= int(v_now) <= 7) or (20 <= int(v_now) <= 23):
                c_brightness = 65
            else:
                c_brightness = 55
            camera.brightness = c_brightness
            camera.contrast = c_contrast
            camera_file = ("/home/william/Documents/Python_Projects/hour" + str(v_now) + "/" + "img_" + str(photo_count) + ".jpg")
            spyder_copy = ("/mnt/raspberrypi/pitest/hour" + str(v_now) + "/" + "img_" + str(photo_count) + ".jpg")
            camera.capture(camera_file)
            copyFile(camera_file,spyder_copy)
            time.sleep(3)
            photo_count = photo_count + 1
    except OSError as err:
        print("OS error: {0}".format(err))
    except ValueError:
        print("Could not convert data to an integer.")
#    except IOError as e:
#        print('Error: %s' % e.strerror)
#        camera.close()
#        sys.exit(1)
    except:
        print("Unexpected error:", sys.exc_info()[0])
        camera.close()
        sys.exit(1)

    v_now = dt.now().strftime("%H")
    v_ymd = dt.now().replace(minute=00, second=00) + td(hours=1)

camera.close()

ktb
Posts: 1380
Joined: Fri Dec 26, 2014 7:53 pm

Re: How to write or delete files on a Windows shared folder

Thu May 21, 2015 11:51 pm

Thank you. I'm probably not the best person to assist with this problem as I do not have a Pi camera module.

I took a quick look at the documentation ( http://picamera.readthedocs.org/en/release-1.10/ ) for picamera which leads me to wonder how you installed it. APT? User installation? System installation? It's possible the permissions/access problem has nothing to do with picamera. It seems like there is a potential for issues with any of these:
- os.mkdir
- camera.capture
- copyFile

famousdavis
Posts: 16
Joined: Tue Mar 24, 2015 8:55 pm

Re: How to write or delete files on a Windows shared folder

Fri May 22, 2015 12:19 am

Well, it's hard to remember exactly what I did, but I remember thinking it was surprisingly simple (I don't know Linux). After installing the hardware onto the Pi, I ran sudo apt-get update and then upgrade, raspi-config to enable the camera, and opened the Python3 IDE to copy some script I Googled to try capturing an image (it worked). That was done all under the Pi account, afterwhich I created user accounts and gave them superuser permissions (again, following something I found off Google).

I'm not too worried about this. Adding uid=william lets me use the Windows share folder using File Manager, which is very convenient now. And instead of running my image-capturing script from the Python shell, I run it command line using sudo, and it works, so I'm exploring new things to try-out now (I wrote a script that now reboots my Pi if I update a Windows file called reboot.txt, so the Pi reboots if it either loses WiFi connection or if I expressly tell it to without logging into my Pi -- my Pi runs headless using Real VNC).

ljylxl
Posts: 4
Joined: Tue May 30, 2017 3:02 pm

Re: How to write or delete files on a Windows shared folder

Thu Sep 07, 2017 2:17 am

famousdavis wrote:
Fri May 22, 2015 12:19 am
Well, it's hard to remember exactly what I did, but I remember thinking it was surprisingly simple (I don't know Linux). After installing the hardware onto the Pi, I ran sudo apt-get update and then upgrade, raspi-config to enable the camera, and opened the Python3 IDE to copy some script I Googled to try capturing an image (it worked). That was done all under the Pi account, afterwhich I created user accounts and gave them superuser permissions (again, following something I found off Google).

I'm not too worried about this. Adding uid=william lets me use the Windows share folder using File Manager, which is very convenient now. And instead of running my image-capturing script from the Python shell, I run it command line using sudo, and it works, so I'm exploring new things to try-out now (I wrote a script that now reboots my Pi if I update a Windows file called reboot.txt, so the Pi reboots if it either loses WiFi connection or if I expressly tell it to without logging into my Pi -- my Pi runs headless using Real VNC).
Hi, I faced the same problem. Would you please tell me where you put the uid=william in your code?
Thank you.

User avatar
DougieLawson
Posts: 35557
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: How to write or delete files on a Windows shared folder

Sat Sep 09, 2017 10:33 am

It goes in the /etc/fstab line for the non-Unix filesystem.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Beginners”