Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error 13 permission
#1
Hello,
Im slowly getting into the game of python and learning its much strict than other languages. Im running into an issue with my script Im working on where when I try to write the file im getting an error 13 ona linux server. The folder in which Im putting the output text into has a permissions of 7777 so anyone can read and write to it. Can anyone help me out as to what I am doing wrong?

#! /usr/bin/python3.6
import pymysql
import datetime
from pncnetworkdata import *
from datetime import date 


try:
    markets = ["Beltway", "Keystone", "Freedom", "New England"]
    count = 0
    cursorRowCount = 0
    recordPrint = ""
    for HOST in HOSTS:
        db_con = pymysql.connect(host=HOST, port=3306, user=USERNAME, passwd=PASSWORD, db=DATABASE) 
        sql_select_Query = "SELECT * FROM pncnetwork where id=(SELECT MAX(id) FROM pncnetwork)"
        cursor = db_con.cursor()
        cursor.execute(sql_select_Query)
        records = cursor.fetchall()
        for row in records:
          # recordPrint = ""
            recordPrint += markets[count] + ', ' + str(row[0]) + ', ' + str(row[5]) + "\n" 
        cursorRowCount += cursor.rowcount
        count += 1
        db_con.close()
#    print ("Total number of rows in pncnetwork is: ", cursorRowCount)
    print ("\nPrinting each pncnetwork record\n", recordPrint)



# This section will print the script to file using current date.


    def get_filename_datetime():
    # Use current date to get a text file name.
     return "file-" + str(date.today()) + ".txt"

#Get full path for writing 
    name = get_filename_datetime()
    print("NAME", name)

    path = "/opt/scripts/output" + name       
    print("PATH", path);

    with open(path, "w") as f:
    # Write Data to file
     f.write("\nPrinting each pncnetwork record\n", recordPrint)

except Exception as e:
    print("ERROR ", str(e))
the script runs and prints to the console window however im getting this error.

[tpolim001@netengsrv01 ~]$ ./pncnetworktest.py

Printing each pncnetwork record
Beltway, 1330, 2020-04-08_1330
Keystone, 1301, 2020-04-08_1301
Freedom, 1246, 2020-04-08_1246
New England, 1051, 2020-04-08_1051

NAME file-2020-04-09.txt
PATH /opt/scripts/outputfile-2020-04-09.txt
ERROR [Errno 13] Permission denied: '/opt/scripts/outputfile-2020-04-09.txt'
[tpolim001@netengsrv01 ~]$

as you can see the folder has read-write global permissions

[tpolim001@netengsrv01 scripts]$ ls -la
total 16
drwxr-xr-x 3 root root 115 Apr 9 11:50 .
drwxr-xr-x. 8 root root 92 Apr 3 14:16 ..
drwsrwsrwt 2 root root 6 Apr 9 11:50 output<<<<<<<<<<<<<<<<<<<<<<
-rwxr-xr-x 1 root root 426 Apr 7 14:56 pncnetworkdata.py
-rwxr-xr-x 1 root root 2118 Apr 7 14:55 pncnetworkold1.py
-rwxr-xr-x 1 root root 2178 Apr 1 14:57 pncnetworkold.py
-rwxr-xr-x 1 root root 957 Apr 7 14:56 pncnetwork.py
[tpolim001@netengsrv01 scripts]$
Reply
#2
(Apr-09-2020, 05:49 PM)tpolim008 Wrote: The folder in which Im putting the output text into has a permissions of 7777 so anyone can read and write to it
I think there is a 7 too much in your permissions. Now you also have setUID and setGID and sticky bit. You don't want that. Use chmod 777 or 0777.
Who is executing the script? Also root?
Reply
#3
(Apr-09-2020, 06:18 PM)ibreeden Wrote:
(Apr-09-2020, 05:49 PM)tpolim008 Wrote: The folder in which Im putting the output text into has a permissions of 7777 so anyone can read and write to it
I think there is a 7 too much in your permissions. Now you also have setUID and setGID and sticky bit. You don't want that. Use chmod 777 or 0777.
Who is executing the script? Also root?
Any user can run the script, I think I found the issue had two problems and have updated my code to the following. Im not sure if this is the most sensable way to do so but it appears to be working. If you have any change suggestions im open to them

the file folder is /opt/scripts/output where I have forgot the last / it should have been /opt/scripts/output/

next was I gave two arguments where I guess this write function can only have one,

ERROR write() takes exactly one argument (2 given)


here is the new code.

#! /usr/bin/python3.6
import pymysql
import datetime
from pncnetworkdata import *
from datetime import date 


try:
    markets = ["Beltway", "Keystone", "Freedom", "New England"]
    count = 0
    cursorRowCount = 0
    recordPrint = ""
    for HOST in HOSTS:
        db_con = pymysql.connect(host=HOST, port=3306, user=USERNAME, passwd=PASSWORD, db=DATABASE) 
        sql_select_Query = "SELECT * FROM pncnetwork where id=(SELECT MAX(id) FROM pncnetwork)"
        cursor = db_con.cursor()
        cursor.execute(sql_select_Query)
        records = cursor.fetchall()
        for row in records:
          # recordPrint = ""
            recordPrint += markets[count] + ', ' + str(row[0]) + ', ' + str(row[5]) + "\n" 
        cursorRowCount += cursor.rowcount
        count += 1
        db_con.close()
#    print ("Total number of rows in pncnetwork is: ", cursorRowCount)
    print ("\nPrinting each pncnetwork record\n", recordPrint)



# This section will print the script to file using current date.


    def get_filename_datetime():
    # Use current date to get a text file name.
     return "file-" + str(date.today()) + ".txt"

#Get full path for writing 
    name = get_filename_datetime()
    print("NAME", name)

    path = "/opt/scripts/output/" + name
    print("PATH", path);

    with open(path, "w") as f:
    # Write Data to file
     f.write (recordPrint)                                      

except Exception as e:
    print("ERROR ", str(e))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  KivyMD android app - problem with permission polak7gt 0 248 Jan-18-2024, 01:27 PM
Last Post: polak7gt
  Potential Permission error on Mac OSX Catalina OWOLLC 1 644 Nov-02-2023, 07:52 AM
Last Post: unjnsacih
  logging: change log file permission with RotatingFileHandler erg 0 959 Aug-09-2023, 01:24 PM
Last Post: erg
  The INSERT permission was denied on the object Steven5055 2 1,426 Feb-25-2023, 11:37 PM
Last Post: Steven5055
  Permission issue when using scapy jao 3 9,462 Feb-05-2022, 06:14 PM
Last Post: snippsat
  Error no 13: Permission denied in python shantanu97 1 6,097 Mar-31-2021, 02:15 PM
Last Post: snippsat
  How a Mac OS software can restart itself with admin permission in Python 3.7? Formationgrowthhacking 0 1,740 Sep-03-2020, 05:29 PM
Last Post: Formationgrowthhacking
  Fixing "PermissionError: [Errno 13] Permission denied" puredata 17 71,959 Mar-09-2020, 03:20 PM
Last Post: syssy
  PermissionError: [Errno 13] Permission denied: error leviathan54 2 46,572 Apr-20-2019, 12:51 AM
Last Post: leviathan54
  [python] PermissionError: [Errno 13] Permission denied akamouch 1 42,393 Feb-07-2019, 03:28 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020