(Python) Issues with directories that have special characters

2024/9/20 5:31:11
  • OS: Windows server 03
  • Python ver: 2.7

For the code below, its runs fine when I substitute "[email protected]" with "fuchida". If I use the email format for directory name I get the following error "WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect:" . Please let me know what I can do to get this to work, my money is on the "@" symbol fudging things up but I do not know how to resolve it in python so far.

import osdef dirListing():dirList = os.listdir("C:\\Program Files\home\Server\Logs\[email protected]")for fname in dirList:print fnamereturndef main():dirListing()if __name__ == '__main__':main()
Answer

I suspect problems with your \ as escape characters. Try this:

import osdef dirListing():dirList = os.listdir(r"C:\\Program Files\home\Server\Logs\[email protected]")for fname in dirList:print fnamereturndef main():dirListing()if __name__ == '__main__':main()
https://en.xdnf.cn/q/72533.html

Related Q&A

LibCST: Converting arbitrary nodes to code

Is it possible to dump an arbitrary LibCST node into Python code? My use case is that I want to extract the code for functions that match a specific naming scheme. I can extract the FunctionDef nodes …

calculating the number of k-combinations with and without SciPy

Im puzzled by the fact that the function comb of SciPy appears to be slower than a naive Python implementation. This is the measured time for two equivalent programs solving the Problem 53 of Project E…

How to subclass requests in python through inheritance

I would like to specialize / subclass the requests package to add some method with custom functionality.I tried to do this:# concrete_requests.py import requestsclass concreteRequests(requests):def __i…

ipython debugger: full traceback on interactive pdb?

I recently switched from ipython0.10 to ipython0.11. In ipython0.11, I only see a small snippet of the full traceback when the python debugger engages (i.e. using %pdb), whereas in ipython0.10 Id see …

How to get all users in a list Twitter API?

Is there a way to access all members in a list? Currently, I can only see the first 20 members? Specifically, Im using python and tweepy.

Python adding a blank/empty column. csv

Hello I have a database that i am trying to make a .csv file quickly from.my data looks like this.Song_Name,File_Name,Artist_Name,Artist_ID Song1,filename1,artistname,artist001 Song1,filename1,artistna…

Displaying Radio buttons horizontally in matplotlib

I am using the matplotlib.widgets to create radio buttons in my widgets, the buttons coming are stacked vertically, I would like them to be stacked horizontally.MVCE:import matplotlib.pyplot as plt fro…

Python MemoryError on large array

This is the python script that Im trying to run:n = 50000000000 ##50 billion b = [0]*n for x in range(0,n):b[x] = random.randint(1,899999)... But the output Im getting is:E:\python\> python sort.py…

How to generate a PDF with non-ascii characters using from_string from python-pdfkit

Im struggling to generate just a simple PDF with non-ascii characters using Python 3.5.2, python-pdfkit and wkhtmltox-0.12.2.This is the easiest example I could write:import pdfkit html_content = u<…

Minimum window of days required to travel all cities

This is an interesting question that I came across in a coding challenge:There are k cities and n days. A travel agent is going to show you city k on day n. Youre supposed to find the minimum number of…