Permission Error when trying to create a .txt file

Problem:
I have not been successful in simply creating a new .txt file onto my desktop.

I am truly at a loss. To be fair, I do not mess with files very much, so I am sure this is a simple solution. One weird thing that I have encountered is that it does create a folder instead of a file even though it has the file extension provided. From what I have researched though, this is very common.

I would not have asked this question if I could not already find it on this website, but I think my situation is a little unique.

I have tried multiple different methods including the following:

  1. Running Idle as administrator.
  2. Creating a folder on the desktop and saving the file there.
  3. Going into each individual .py file and making sure they have permissions.

Code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>desktop_path = pathlib.Path.home() / 'OneDrive' / 'Desktop'
print (desktop_path)
def save_data():
file_name = last+'_'+first+'_'+'Symptom Checklist 90-R Results.txt'
file_path = Path(os.path.join(desktop_path, file_name))
table = tabulate(data, headers="firstrow", tablefmt="grid")
os.makedirs(file_path, exist_ok=True)
print(file_path)
try:
with open(file_path,'w') as file:
file.write(first_name+"n"+last_name+"n"+age+"n"+birthday+"n")
file.write(table)
file.close()
except PermissionError:
print("You do not have access to create this file.")
</code>
<code>desktop_path = pathlib.Path.home() / 'OneDrive' / 'Desktop' print (desktop_path) def save_data(): file_name = last+'_'+first+'_'+'Symptom Checklist 90-R Results.txt' file_path = Path(os.path.join(desktop_path, file_name)) table = tabulate(data, headers="firstrow", tablefmt="grid") os.makedirs(file_path, exist_ok=True) print(file_path) try: with open(file_path,'w') as file: file.write(first_name+"n"+last_name+"n"+age+"n"+birthday+"n") file.write(table) file.close() except PermissionError: print("You do not have access to create this file.") </code>
desktop_path = pathlib.Path.home() / 'OneDrive' / 'Desktop'
print (desktop_path)

def save_data():   
        file_name = last+'_'+first+'_'+'Symptom Checklist 90-R Results.txt'
        file_path = Path(os.path.join(desktop_path, file_name))
        
        table = tabulate(data, headers="firstrow", tablefmt="grid")
        
        os.makedirs(file_path, exist_ok=True)
        print(file_path)
        try:
            with open(file_path,'w') as file:
                file.write(first_name+"n"+last_name+"n"+age+"n"+birthday+"n")
                file.write(table)
            file.close()
        except PermissionError:
            print("You do not have access to create this file.")

Here are what the print statements execute:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>C:UsersnameOneDriveDesktop
C:UsersnameOneDriveDesktop__Symptom Checklist 90-R Results.txt
You do not have access to create this file.
</code>
<code>C:UsersnameOneDriveDesktop C:UsersnameOneDriveDesktop__Symptom Checklist 90-R Results.txt You do not have access to create this file. </code>
C:UsersnameOneDriveDesktop
C:UsersnameOneDriveDesktop__Symptom Checklist 90-R Results.txt
You do not have access to create this file.

Again, I am sure this is a simple solution, and I am just blind to see it. I have also made sure that this file name doesn’t already exist for whatever reason.

5

Maybe try running the python file from admin terminal.

  1. Open the admin terminal by right clicking the start button and then selecting command prompt (admin)
  2. Then run the file from that terminal by: python [filename].py

1

Thank you all for your insight and help. I ended up following this example found here: https://www.codeease.net/programming/python/how-to-run-python-script-as-admin

The first solution provided me with an easy solution. All I had to do was simply add this line: os.system(f'runas /user:Administrator "{file_path}"')

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật